Python 幻影唐';t通过Selenium加载mobile.twitter.com

Python 幻影唐';t通过Selenium加载mobile.twitter.com,python,selenium,twitter,request,phantomjs,Python,Selenium,Twitter,Request,Phantomjs,我的配置-Selenium 2.44.0+Python 3.4.2+PhantomJS 2.0.0(在Windows 7 x64上)。 我尝试使用PhantomJS WebDriver从Python程序加载,但收到错误消息: Traceback (most recent call last): File "C:\Python\Python342x64\myprog\code.py", line 11, in <module> browser.get('https://mo

我的配置-Selenium 2.44.0+Python 3.4.2+PhantomJS 2.0.0(在Windows 7 x64上)。 我尝试使用PhantomJS WebDriver从Python程序加载,但收到错误消息:

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]
如果我使用Firefox而不是PhantomJS,一切都会很好。(或者如果我加载桌面版twitter)
如何使用PhantomJS解决此问题?

尝试将用户代理设置为:

Android/Firefox 29:Mozilla/5.0(Android;移动;rv:29.0) Gecko/29.0 Firefox/29.0


我已经尝试了不同的用户代理设置,但没有帮助:(我尝试:
Mozilla/5.0(Windows NT 6.1;WOW64;rv:35.0)Gecko/20100101 Firefox/35.0“
Mozilla/5.0(Linux;Android 4.2.1;Nexus 4 Build/JOP40D)AppleWebKit/535.19(KHTML,像Gecko)Chrome/18.0.1025.166移动Safari/535.19
Mozilla/5.0(iPhone;CPU-iPhone-OS-5\U-0,像Mac-OS-X)Applewkit/534.46(像Gecko)Version/5.1 Mobile/9A334 Safari/7534.48.3
为什么你要在移动版中绝对使用PJS?因为它比桌面版更轻更快。使用firefox你做不到什么?我无法在屏幕上隐藏firefox窗口,所以我决定使用无头PhantomJS。顺便说一句,PhantomJS只使用15mb内存:)
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')