Python 另一个使用Scrapy的IMDB爬虫程序
在尝试爬网imdb时遇到了一些问题,但在这里没有找到答案 我正在尝试从页面中获取一些数据,如:使用以下代码:Python 另一个使用Scrapy的IMDB爬虫程序,python,scrapy,imdb,Python,Scrapy,Imdb,在尝试爬网imdb时遇到了一些问题,但在这里没有找到答案 我正在尝试从页面中获取一些数据,如:使用以下代码: import scrapy from tutorial.items import MovieItem, CastItem class tutorialSpider(scrapy.Spider): name = "tutorial" allowed_domains = ["imdb.com"] # generate start_urls dynamically
import scrapy
from tutorial.items import MovieItem, CastItem
class tutorialSpider(scrapy.Spider):
name = "tutorial"
allowed_domains = ["imdb.com"]
# generate start_urls dynamically
def start_requests(self):
for year in range(1950, 1951):
for page in range(1, 3):
yield scrapy.Request('http://www.imdb.com/search/title?release_date=%s&page=%s' % (year, page))
def parse(self, response):
self.wanted_num=50
for sel in response.xpath("//*[contains(@class,'lister-item-content')]"):
item = MovieItem()
item['Title'] = sel.xpath('h3/a/text()').extract()[0]
item['Rating'] = sel.xpath('div[@class="ratings-bar"]/div[@name="ir"]/strong/text()').extract()[0]
item['Ranking']=sel.xpath('h3/span[@class="lister-item-index unbold text-primary"]/text()').extract()[0]
item['ReleaseDate'] = sel.xpath('h3/span[@class="lister-item-year text-muted unbold"]/text()').extract()[0]
item['MianPageUrl'] = "http://imdb.com"+sel.xpath('h3/a/@href').extract()[0]
request = scrapy.Request(item['MianPageUrl'], callback=self.parseMovieDetails)
request.meta['item'] = item
if int(item['Ranking']) >= self.wanted_num + 1:
return
yield request
因此,这里的问题是:
感谢您的帮助!您可以发布爬网日志吗?您可以通过
scrapy crawl spider-s log\u FILE=output.log
或scrapy crawl spider&>output.log
命令来发布爬网日志。您好,奇怪的事情发生了:-)现在它可以工作了!至于问题#2,我修正了它:item['Ranking']=re.match(r'(^[0-9]+)、sel.xpath('h3/span[@class=“lister item index unbold text primary”]/text()).extract()[0]。\uu str_uuuuuuuuuuuuuuuuuuuuuuuuu().strip())。group(1)
FYI:scrapy选择器已经内置了regex
快捷方式,例如:sel.xpath(///div').re('someregex'),谢谢!我不知道