我的配置 - Selenium 2.44.0 + Python 3.4.2 + PhantomJS 2.0.0(在Windows 7 x64上)。 我尝试使用PhantomJS WebDriver从Python程序加载https://mobile.twitter.com,我收到错误消息:
Traceback (most recent call last):
File "C:\Python\Python342x64\myprog\code.py", line 11, in <module>
browser.get('https://mobile.twitter.com/')
File "C:\Python\Python342x64\lib\site-packages\selenium-2.44.0-py3.4.egg\selenium\webdriver\remote\webdriver.py", line 185, in get
self.execute(Command.GET, {'url': url})
File "C:\Python\Python342x64\lib\site-packages\selenium-2.44.0-py3.4.egg\selenium\webdriver\remote\webdriver.py", line 171, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Python\Python342x64\lib\site-packages\selenium-2.44.0-py3.4.egg\selenium\webdriver\remote\remote_connection.py", line 349, in execute
return self._request(command_info[0], url, body=data)
File "C:\Python\Python342x64\lib\site-packages\selenium-2.44.0-py3.4.egg\selenium\webdriver\remote\remote_connection.py", line 417, in _request
resp = opener.open(request)
File "C:\Python\Python342x64\lib\urllib\request.py", line 455, in open
response = self._open(req, data)
File "C:\Python\Python342x64\lib\urllib\request.py", line 473, in _open
'_open', req)
File "C:\Python\Python342x64\lib\urllib\request.py", line 433, in _call_chain
result = func(*args)
File "C:\Python\Python342x64\lib\urllib\request.py", line 1202, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\Python\Python342x64\lib\urllib\request.py", line 1177, in do_open
r = h.getresponse()
File "C:\Python\Python342x64\lib\http\client.py", line 1172, in getresponse
response.begin()
File "C:\Python\Python342x64\lib\http\client.py", line 351, in begin
version, status, reason = self._read_status()
File "C:\Python\Python342x64\lib\http\client.py", line 313, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "C:\Python\Python342x64\lib\socket.py", line 371, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054]
我的计划代码:
import os, sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import NoSuchElementException
from random import randint
from time import sleep
browser = webdriver.PhantomJS('C:\Program Files (x86)\PhantomJS\phantomjs.exe')
browser.get('https://mobile.twitter.com/')
sleep(randint(5,10))
browser.save_screenshot('out1.png')
g = open('C:\\Python\\Python342x64\\prog\\page.htm', 'a', encoding='utf8', errors='replace')
g.write(browser.page_source + '\n')
如果我使用Firefox而不是PhantomJS,一切正常。 (或者,如果我加载桌面版的推特) 我如何用PhantomJS解决这个问题?
答案 0 :(得分:1)
尝试将用户代理设置为:
Android / Firefox 29:Mozilla / 5.0(Android;移动版; rv:29.0) Gecko / 29.0 Firefox / 29.0