【问题标题】:Data crawling using scrapy package in python在python中使用scrapy包进行数据爬取
【发布时间】:2017-09-22 04:39:31
【问题描述】:
  1. 我正在尝试使用“scrapy”包从网站 (IMDB) 获取一些带有图像的数据。

  2. 如果 div 类中有 image_URL,那么我可以使用电影海报抓取数据。但是,如果没有,我的代码将无法正常工作。它跳过了一些与图像相关的数据。

  3. 我想修复它,就像没有 image_URL 一样,然后忘记图像,只抓取数据。

  4. 除了部分,我该如何修复?

def 解析(自我,响应):

//some other lines

try:
        poster_image_url = 
        response.xpath('//div[@class="poster"]/a/img/@src').extract()[0]
        poster_image_url = [ poster_image_url.split("_V1_")[0] + "_V1_.jpg" ]

except:
        poster_image_url = None
        item['image_urls'] = poster_image_url

这是管道代码↓↓↓↓

类 ImdbPipeline(对象):

def process_item(self, item, spider):
    return item

def get_media_requests(self, item, info):
    for image_url in item['image_urls']:
        yield scrapy.Request(image_url)

【问题讨论】:

    标签: python python-2.7 scrapy scrapy-spider scrapy-pipeline


    【解决方案1】:

    您可以将extract_first() 与 if 检查一起使用:

    poster_image_url = response.xpath('//div[@class="poster"]/a/img/@src').extract_first()
    if poster_image_url:
        item['image_urls'] = poster_image_url.split('_V1')[0] + '_V1_.jgp'
    

    您也可以使用scrapy ItemLoader's

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-12
      • 2012-06-12
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多