要明确的是,我尝试抓取有关赌场的论坛,目前,我已经使用与下面相同的方案成功地做到了这一点:
class test_spider(scrapy.Spider):
count=0
name = "test_spyder"
start_urls = [
'https://casinogrounds.com/forum/search/?&q=Casino&search_and_or=or&sortby=relevancy',
]
rules = ( Rule(LinkExtractor(restrict_css=('a:contains("Next")::attr(href)')), callback='parse') )
def parse(self, response) :
print(self.count)
for href in response.css("span.ipsType_break.ipsContained a::attr(href)") :
new_url = response.urljoin(href.extract())
#print(new_url)
yield scrapy.Request(new_url, callback = self.parse_review)
next_page = response.css('a:contains("Next")::attr(href)').extract_first()
print(next_page)
if next_page is not None:
yield scrapy.Request(next_page, callback = self.parse)
def parse_review(self, response):
parsed_uri = urlparse(response.url)
domain = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri)
for review in response.css('article.cPost.ipsBox.ipsComment.ipsComment_parent.ipsClearfix.ipsClear.ipsColumns.ipsColumns_noSpacing.ipsColumns_collapsePhone') :
yield {
'name': review.css('strong a.ipsType_break::text').extract_first(),
'date': review.css('time::attr(title)').extract_first(),
'review': review.css('p::text').extract(),
'url' : response.url
}
next_page = response.css('li.ipsPagination_next a::attr(href)').extract_first()
if next_page is not None:
yield response.follow(next_page, callback=self.parse_review)
因此,当我在 python 脚本中执行该蜘蛛时,通常(我的意思是对于其他论坛)它会从起始 URL 开始抓取所有页面的所有线程。
但对于那个,它不会,它只废弃第一页的所有线程,它获取进入第二页的正确 URL,但会再次调用解析函数。
当然,如果我将所有页面的 URL 放入 start_urls 列表中,它会废弃所有页面...
感谢您的帮助。
最佳答案
您收到的 HTTP 429 响应意味着该网站正在限制您的请求以避免被淹没。您可以使用the AutoThrottle extension将您的请求频率限制在网站允许的范围内。
关于python - 使用 scrapy 进行网页抓取论坛不会产生下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51264283/