我今天刚开始使用Scrapy,但我以前有过javascript编程经验,所以请耐心听我说,我会给出一个非常详细的解释:
我用一个gramReport来分析instagram的一些个人资料(提取关注者的数量、帖子的数量和其他数据),因为我有一个不同的配置文件列表,我想自动化这个任务
最后的想法是这样的:
1. Use Scrapy to crawl a specific profile ( so append 'profile' to 'gramreport.com/user/' )
2. Extract specific data and save it in a csv
我认为python可以胜任这项工作,开始搜索并发现scrapy,文档非常适合我。 https://doc.scrapy.org/en/latest/intro/tutorial.html
我决定尝试一下,就像教程一样,我创建了一个蜘蛛:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "profile"
start_urls = [
'http://gramreport.com/user/cats.gato'
]
def parse(self, response):
page = response.url.split("/")[-1]
filename = 'profile-%s.html' % page
with open(filename, 'wb') as f:
f.write(response.body)
所以scrapy crawl profile
工作得很好,我无法获得html页面。
接下来我尝试使用shell:
scrapy shell 'http://gramreport.com/user/cats.gato'
很好,我可以通过Xpath或CSS获得一些数据:
//Followers:
response.xpath('/html/body/div[3]/table[1]/tr/td[2]/table/tr[1]/td/div/table/tr[2]/td/text()').extract()
//Posts:
response.xpath('/html/body/div[3]/table[1]/tr/td[2]/table/tr[3]/td/div/table/tr[2]/td/text()').extract()
//Page Name:
response.xpath('/html/body/div[3]/table[1]/tr/td[1]/div/div/div/span[2]/text()').extract()
//Average Likes:
response.xpath('/html/body/div[3]/div[1]/div/div/div[1]/div/text()').extract()
//Average Comments:
response.xpath('/html/body/div[3]/div[1]/div/div/div[2]/div/text()').extract()
我得到的大多数结果都有u'字符和其他正则表达式,比如[u'\n\t\t\t252,124\t\t']
,但我认为已经有人回答了这个问题。你知道吗
但是,有一些数据我无法提取,我只是没有得到任何结果
首先是Recent Interactions
表,这是由于AJAX造成的,但我不明白如何修复它;也许使用延迟?你知道吗
其次是Top Hashtags
和Top User Mentions
表
他们的xpath不起作用,css选择器也不起作用;我不明白为什么。你知道吗
页面加载时会发出AJAX请求。你知道吗
如果在加载页面时打开web检查器,您将看到如下AJAX请求:
如果在page source中按住ctrl+f组合键,您将看到一些javascript,如:
您可以使用scrapy找到此url,然后转发请求:
相关问题 更多 >
编程相关推荐