【发布时间】:2019-04-23 17:40:08
【问题描述】:
我正在抓取网站“https://www.imdb.com/title/tt4695012/reviews?ref_=tt_ql_3”。我需要的数据是来自上述网站的评论和评级。 我只能爬2页。 但我想要网站所有页面的评论和评分。
下面是我试过的代码
我在 start_urls 中包含了多个网站。
class RatingSpider(Spider):
name = "rate"
start_urls = ["https://www.imdb.com/title/tt4695012/reviews?ref_=tt_ql_3"]
def parse(self, response):
ratings = response.xpath("//div[@class='ipl-ratings-bar']//span[@class='rating-other-user-rating']//span[not(contains(@class, 'point-scale'))]/text()").getall()
texts = response.xpath("//div[@class='text show-more__control']/text()").getall()
result_data = []
for i in range(0, len(ratings)):
row = {}
row["ratings"] = int(ratings[i])
row["review_text"] = texts[i]
result_data.append(row)
print(json.dumps(row))
next_page = response.xpath("//div[@class='load-more-data']").xpath("@data-key").extract()
next_url = response.urljoin("reviews/_ajax?ref_=undefined&paginationKey=")
next_url = next_url + next_page[0]
if next_page is not None and len(next_page) != 0:
yield scrapy.Request(next_url, callback=self.parse)
帮助我抓取网站的所有页面。
【问题讨论】: