【发布时间】:2018-07-10 11:41:41
【问题描述】:
为了清楚,我尝试爬取有关赌场的论坛,目前,我已经成功地使用了以下相同的方案:
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,但确实会再次调用 parse 函数。
当然,如果我将页面的所有 URL 都放在 start_urls 列表中,它会报废所有页面...
感谢您的帮助。
【问题讨论】:
-
当你说它得到正确的 URL 时,你是说
print(next_page)显示正确的 URL? -
是的,这完全正确:casinogrounds.com/forum/search/…
-
那个页面没有
print(self.count)? -
不,来自 start_urls 的 URL 是 self.count 然后显示正确的下一个 URL,这要归功于 print(next_page) 但随后抓取结束。我还会注意到,有时它会转到第二页,然后从这里结束。
-
上次运行告诉我:>>>>> 开始抓取..... casinogrounds.com/forum/search/… 0 casinogrounds.com/forum/search/… >>>>> 抓取结束!
标签: python scrapy web-crawler