如何在我自动打开网站时避免连接错误?

问题描述:

我使用Python 2.7在Debian上删除了几个网站,但也许我的代码会自动停止(如果无法加载(冻结)或没有Internet连接)。如何在我自动打开网站时避免连接错误?

有没有解决方案可以解决这个问题,也许只是跳过问题并运行我的代码到下一个URL?因为如果我得到这样一个问题,这个脚本只是自动停止..

这里我的代码:

#!/usr/bin/python 
#!/bin/sh 
# -*- coding: utf-8 -*- 
from bs4 import BeautifulSoup 
from selenium import webdriver 
import urllib2 
import subprocess 
import unicodecsv as csv 
import os 
import sys 
import io 
import time 
import datetime 
import pandas as pd 
import MySQLdb 
import re 
import contextlib 
import selenium.webdriver.support.ui as ui 
import numpy as np 
from datetime import datetime, timedelta 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 
import pyautogui 
from pykeyboard import PyKeyboard 

reload(sys) 
sys.setdefaultencoding('utf-8') 


cols = ['MYCOLS..'] 

browser = webdriver.Firefox() 
datatable=[] 

browser.get('LINK1') 
time.sleep(5) 

browser.find_element_by_xpath('//button[contains(text(), "CLICK EVENT")]').click() 
time.sleep(5) 
browser.find_element_by_xpath('//button[contains(text(), "CLICK EVENT")]').click() 
html = browser.page_source 
soup=BeautifulSoup(html,"html.parser") 
table = soup.find('table', { "class" : "table table-condensed table-hover data-table m-n-t-15" })  

for record in table.find_all('tr', class_="hidden-xs hidden-sm ng-scope"): 
    for data in record.find_all("td"): 
     temp_data.append(data.text.encode('utf-8')) 
    newlist = filter(None, temp_data) 
    datatable.append(newlist) 

time.sleep(10) 
browser.close() 

#HERE I INSERT MY DATAES INTO MYSQL..IT IS NOT IMPORTANT, AND MY SECOND LINK STARTING HERE 

browser = webdriver.Firefox() 
datatable=[] 

browser.get('LINK2') 
browser.find_element_by_xpath('//button[contains(text(), "LCLICK EVENT")]').click() 
time.sleep(5) 
html = browser.page_source 
soup=BeautifulSoup(html,"html.parser") 
table = soup.find('table', { "class" : "table table-condensed table-hover data-table m-n-t-15" }) 

for record in table.find_all('tr', class_="hidden-xs hidden-sm ng-scope"): 
    for data in record.find_all("td"): 
     temp_data.append(data.text.encode('utf-8')) 
    newlist = filter(None, temp_data) 
    datatable.append(newlist) 

time.sleep(10) 
browser.close() 

#MYSQLDB PART AGAIN...AND THE NEXT LINK IS COMING. 

+1编辑:

当脚本找不到这个Click事件停歇过。为什么?我怎样才能避免这种情况?

+0

两两件事,1。使你的代码建模1,使用异常处理 –

+0

欢迎堆栈溢出!请参阅:[我如何做X?](https://meta.*.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-dox )对SO的期望是,用户提出的问题不仅仅是研究来回答他们自己的问题,而且还分享研究,代码尝试和结果。这表明你已经花时间去尝试帮助自己,它使我们避免重申明显的答案,最重要的是它可以帮助你得到更具体和相关的答案!另请参阅:[问] – JeffC

使用Selenium,您可以配置驱动程序(浏览器对象)以等待特定的元素或条件。然后,您可以使用常规尝试/除了处理任何错误,例如TimeoutException或许多其他。

Selenium在their documentation上很好地解释了等待系统。

这里是异常处理的代码片段硒:

from selenium.webdriver.support.ui import WebDriverWait 
from selenium.common.exceptions import TimeoutException 
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 

try: 
    # Wait for any element/condition, you can even had lambda if you wish to 
    WebDriverWait(browser, 10).until(
     EC.visibility_of_all_elements_located((By.ID, 'my-item')) 
    ) 
except TimeoutException: 
    # Here I raise an error but you can do whatever you want like exiting properly or logging something 
    raise RuntimeError('No Internet connection') 
+0

例如,我可以等待一个按钮?这是示例网页:https://www.flightradar24.com/data/airports/grz/departures和我的代码:browser.find_element_by_xpath('// button [contains(text(),“加载更早的航班”)]'' ).click(),我怎样才能将它构建到我的代码中,以及我必须放在哪里? – Harley

+0

我不会为你做任何事情,我给你一个小费。现在使用Selenium,你可以很容易地构建,看到他们的文档 – rak007

+0

我刚刚问了一个正确的例子..无论如何感谢你。 – Harley