gpt4 book ai didi

python - 为什么 socket 会干扰 Selenium ?

转载 作者:行者123 更新时间:2023-12-03 11:53:42 25 4
gpt4 key购买 nike

我编写了一个python脚本,以使用套接字(Checking network connection)检查互联网连接,然后使用selenium从yahoo finance抓取html。

非常频繁(但并非总是如此),它给出了ReadTimeoutError(请参见下文)

我可以改为使用http.client检查互联网连接来使其工作(请参阅下文),但是我仍然想知道为何套接字会干扰 Selenium 。


def internet(host="8.8.8.8", port=443, timeout=1):
try:
socket.setdefaulttimeout(timeout)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.shutdown(socket.SHUT_RDWR)
s.close()
return True
except OSError:
s.close()
return False

# Wait for internet to be available

i = 1
while internet() is False:
time.sleep(1)
if i == 300: # quit if no connection for 5 min (300 seconds)
print('\nIt has been 5 minutes. Aborting attempt.\n')
sys.exit(0)
i += 1

# Get html from yahoo page

symb = 'AAPL'
url = 'http://finance.yahoo.com/quote/{}/history'.format(symb)

chop = webdriver.ChromeOptions()
chop.add_argument('--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Firefox/68.0"')
driver = webdriver.Chrome('/Users/fake_user/Dropbox/Python/chromedriver', chrome_options=chop)
driver.get(url)
html_source = driver.page_source
driver.quit()

它引发此错误:

urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='127.0.0.1', port=58956): Read timed out. (read timeout=<object object at 0x103af7140>)



我可以更改Internet功能作为一种解决方法,但是我不知道 为什么套接字会干扰 Selenium :

import http.client as httplib

def internet():
conn = httplib.HTTPConnection("www.google.com", timeout=5)
try:
conn.request("HEAD", "/")
conn.close()
return True
except:
conn.close()
return False

最佳答案

documentation:

socket.setdefaulttimeout(timeout)

Set the default timeout in seconds (float) for new socket objects. When the socket module is first imported, the default is None. See settimeout() for possible values and their respective meanings.



问题是 setdefaulttimeout为所有新创建的套接字设置了超时,因此也为Selenium设置了超时。这是全局套接字库设置。

如果只想对此套接字实例使用超时,请使用 socket.settimeout(value)( doc)。
def internet(host="8.8.8.8", port=443, timeout=1):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout) # correction from s.timeout(timeout)
s.connect((host, port))
s.shutdown(socket.SHUT_RDWR)
s.close()
return True
except OSError:
s.close()
return False

关于python - 为什么 socket 会干扰 Selenium ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57167357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com