我想知道有没有办法不用他们的API就能抓取twitter?我试着使用他们的API,它很棒。不过,我想问一问,是否有其他选择?由于我正在处理的爬虫将被传递,我不希望我的令牌密钥在它们之间共享。我也不希望他们每个人都经历创建Dev帐户的麻烦,诸如此类。
我用twitter API创建的爬虫能够检索很多很多tweet。而我创建的爬虫只能爬行10条左右,因为其他tweet会在html之外。
我使用的是python 3.6
def spider(targetname, DOMAIN):
for item in g_data:
try:
name = item.find_all("strong", {"class": "fullname show-popup-with-id "})[0].text
username = item.find_all("span", {"class": "username u-dir"})[0].text
post = item.find_all("p", {"class": "TweetTextSize TweetTextSize--normal js-tweet-text tweet-text"})[0].text
retweetby = item.find_all("a", {"href": "/"+targetname})[0].text
subdatas = item.find_all('div', {'class':'ProfileTweet-actionCountList u-hiddenVisually'})
for subdata in subdatas:
replies = subdata.find_all("span", {"class": "ProfileTweet-actionCountForAria"})[0].text
retweets = subdata.find_all("span", {"class": "ProfileTweet-actionCountForAria"})[1].text
likes = subdata.find_all("span", {"class": "ProfileTweet-actionCountForAria"})[2].text
datas = item.find_all('a', {'class':'tweet-timestamp js-permalink js-nav js-tooltip'})
for data in datas:
link = DOMAIN + data['href']
date = data['title']
if link in open(crawledfile).read():
pass
else:
append_to_crawled(crawledfile, name, username, post, link, replies, retweets, likes, retweetby, date)
output(name, username, post, link, replies, retweets, likes, retweetby, date)
except:
pass发布于 2017-06-02 12:18:59
是一种在不使用twitter API的情况下爬行/ scrape twitter的方法;但是,强烈建议您使用API本身的。这有几个优点,比如it是官方的,另外还有来自社区的大量支持。
不过,您可以使用requests和beautiful soup执行爬行,或者如果您正在寻找更强大的选项,请使用Selenium和PhantomJS。
这里有几个类似的问题,你可以通读一下:
Scraping of the Twitter follower page using selenium and phantomjs
How to collect tweets about an event that are posted on specific date using python?
How to perform oauth when doing twitter scraping with python requests
干杯:)
https://stackoverflow.com/questions/44320591
复制相似问题