我有以下代码:
driver = webdriver.Firefox()
for element in links:
driver.get(element)
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
#driver.switchTo().window()
driver.close()
date = soup.find_all("td", {"id": "utime"})
title = soup("title")
链接是我需要解析的网址列表。链接的第一个元素是okey但第二个链接打开时;
Traceback (most recent call last):
File "/home/Version01.py", line 10, in <module>
driver.get(element)
File "/usr/local/lib/python2.7/dist-packages
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver /remote/webdriver.py", line 199, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver /remote/remote_connection.py", line 395, in execute
return self._request(command_info[0], url, body=data)
.......
File "/usr/lib/python2.7/httplib.py", line 778, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
如何浏览阵列中的不同链接?
答案 0 :(得分:4)
你在for循环中关闭driver
,因此第二次迭代不再适用于它。
尝试在for循环之后使用driver.close()
:
driver = webdriver.Firefox()
for element in links:
driver.do_something()
driver.close()