weibo-search
weibo-search copied to clipboard
关于重复爬取的问题
作者您好! 在关键词爬取微博的时候,我设置爬取时间为2020-01-01到2020-01-30,但是在爬取一段时间以后,程序就一直重复爬取2020-01-24的微博,数据库里面的数据量一直没有增加。这种情况应该怎么办呢?
感谢反馈。
是不是只得到1到24的微博?如果是,停止程序,设置从25爬到30,不知道能不能得到新微博。重新获取的时候最好删除crawls文件夹的进度文件,它记录的是上次的进度。
不是,是程序就在24号的10-11点里面重复爬取,控制台能够输出爬取的信息,信息显示一直在爬取10-11点相同的微博内容。
如果方便能否告知关键词?
关键词为"新冠肺炎"。 然后我手动修改为爬取2020-01-25到2020-01-30的微博信息,刚开始也是正常,在爬取大约一万条数据以后,控制台显示又开始重复爬取25号12点45分左右的数据,因为是重复数据,所以数据库数量就不再增长。
我测试了关键词,目前没有出错。怀疑是不是速度加快之后,链接不同页面相同,导致出错。减慢速度看看是否有效。
好的,非常感谢作者!
作者您好! 我设置DELAY = 10,关键词为“新冠肺炎”,时间为2020-01-25到2020-01-25,其他为默认设置然后写入数据库仍然出现重复爬取的问题。
您要是有时间是否方便测试一下,谢谢您!
修改search.py的parse_by_hour为:
def parse_by_hour(self, response):
"""以小时为单位筛选"""
keyword = response.meta.get('keyword')
is_empty = response.xpath(
'//div[@class="card card-no-result s-pt20b40"]')
if is_empty:
print('当前页面搜索结果为空')
else:
# 解析当前页面
for weibo in self.parse_weibo(response):
self.check_environment()
yield weibo
next_url = response.xpath(
'//a[@class="next"]/@href').extract_first()
if next_url:
next_url = self.base_url + next_url
yield scrapy.Request(url=next_url,
callback=self.parse_page,
meta={'keyword': keyword})
问题已经完美解决,十分感谢作者!
您好,我想问一下,按照这个修改了之后在这一段报错undefined name 'start(end)_time’是怎么回事呀?因为我看修改过后的代码和原来的相比删掉了定义开始和结束时间的语句
@ajingjing145 直接用这个代码替换对应的原方法,应该可以的。
问题已经完美解决,十分感谢作者!
是全部替换吗 还是上半部分替换
应该是完全替换;只替换上半部分会报错
修改search.py的parse_by_hour为:
def parse_by_hour(self, response): """以小时为单位筛选""" keyword = response.meta.get('keyword') is_empty = response.xpath( '//div[@class="card card-no-result s-pt20b40"]') if is_empty: print('当前页面搜索结果为空') else: # 解析当前页面 for weibo in self.parse_weibo(response): self.check_environment() yield weibo next_url = response.xpath( '//a[@class="next"]/@href').extract_first() if next_url: next_url = self.base_url + next_url yield scrapy.Request(url=next_url, callback=self.parse_page, meta={'keyword': keyword})
def parse_by_hour(self, response):
"""以小时为单位筛选"""
keyword = response.meta.get('keyword')
is_empty = response.xpath(
'//div[@class="card card-no-result s-pt20b40"]')
start_time = response.meta.get('start_time')
end_time = response.meta.get('end_time')
page_count = len(response.xpath('//ul[@class="s-scroll"]/li'))
if is_empty:
print('当前页面搜索结果为空')
elif page_count < self.further_threshold:
# 解析当前页面
for weibo in self.parse_weibo(response):
self.check_environment()
yield weibo
next_url = response.xpath(
'//a[@class="next"]/@href').extract_first()
if next_url:
next_url = self.base_url + next_url
yield scrapy.Request(url=next_url,
callback=self.parse_page,
meta={'keyword': keyword})
else:
for region in self.regions.values():
url = ('https://s.weibo.com/weibo?q={}®ion=custom:{}:1000'
).format(keyword, region['code'])
url += self.weibo_type
url += self.contain_type
url += '×cope=custom:{}:{}&page=1'.format(
start_time, end_time)
# 获取一小时一个省的搜索结果
yield scrapy.Request(url=url,
callback=self.parse_by_hour_province,
meta={
'keyword': keyword,
'start_time': start_time,
'end_time': end_time,
'province': region
})
大佬,这么长的都修改掉,只留下那么一小段嘛?还是说后面那段else依然保留
@Demonpy1 只留一小段。