2013-02-18 111 views
0

当我尝试微博使用此代码抓取:如何使用Python抓取Twitter页面?

import urllib2 
s = "https://mobile.twitter.com/bing/" 
html = urllib2.urlopen(s).read() 
print html 

...我收到以下错误:

Traceback (most recent call last): 
    File "C:\Users\arpit\Downloads\Desktop\Wiki Code\final Crawler_wiki.py", line 14, in <module> 
    html = urllib2.urlopen(s).read() 
    File "C:\Python27\lib\urllib2.py", line 126, in urlopen 
    return _opener.open(url, data, timeout) 
    File "C:\Python27\lib\urllib2.py", line 400, in open 
    response = self._open(req, data) 
    File "C:\Python27\lib\urllib2.py", line 418, in _open 
    '_open', req) 
    File "C:\Python27\lib\urllib2.py", line 378, in _call_chain 
    result = func(*args) 
    File "C:\Python27\lib\urllib2.py", line 1215, in https_open 
    return self.do_open(httplib.HTTPSConnection, req) 
    File "C:\Python27\lib\urllib2.py", line 1177, in do_open 
    raise URLError(err) 
URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it> 

如果我更换mobile.twitter.comtwitter.com那么它的工作原理,但我希望它与mobile.twitter.com一起工作。

+0

你为什么要抓取手机twitter网站? Twitter有一个记录良好的API,它为您提供了一切,并且有许多Python客户端库。 – 2013-02-19 03:57:13

回答

0

该twitter站点可能正在寻找一个用户代理,当您通过urllib api发出请求时,您没有设置该用户代理。

您可能需要使用诸如mechanize之类的东西来伪造您的用户代理。

但我强烈建议您使用twitter api,它提供了很多简单而又棒的方式来玩数据。

+0

我也尝试过Python-twitter Api和Twython。两者都给出了相同的错误代码。我在一个代理后面,我想那会导致麻烦。 我也为twitter API发布了一个新的http://stackoverflow.com/questions/14951901/unable-to-get-data-using-python-twitter-api。 – 2013-02-19 07:25:37

+0

@ArpitAgarwal我想把这个标记为已经解决了那些想要通过防火墙问题刮掉twitter移动设备的人。 – myusuf3 2013-02-19 14:53:32