【问题标题】:how to scrape content using scrapy which have same class name如何使用具有相同类名的scrapy抓取内容
【发布时间】:2018-02-06 23:26:21
【问题描述】:

我正在使用 scrapy 从 this 网站上抓取数据,但在从具有相同类名的 div 中抓取内容时遇到问题。

<div class="list">
   <a id="followed_by" name="followed_by"></a>
  <h4 class="li_group">Followed by</h4>
  <div class="soda odd"><a href="http://www.imdb.com/title/tt0094450">Dirty Dancing</a></div>
  <div class="soda even"><a href="http://www.imdb.com/title/tt0338096">Dirty Dancing: Havana Nights</a></div>
   <a id="version_of" name="version_of"></a>
  <h4 class="li_group">Version of</h4>
  <div class="soda odd"><a href="http://www.imdb.com/title/tt5262792">Dirty Dancing</a></div>
   <a id="remade_as" name="remade_as"></a>
  <h4 class="li_group">Remade as</h4>
  <div class="soda odd"><a href="http://www.imdb.com/title/tt0461062">Holiday</a></div>
</div>

我尝试使用 xpath,但是当我尝试从多个页面中抓取时遇到了困难。例如,当我试图从this 中抓取时,我用于第一页的 xpath 不起作用。
这是我尝试过的代码:

class ImdbSpider(scrapy.Spider):
    name = "IMDB"
    allowed_domains = ["http://www.imdb.com"]
    start_urls = [l.strip() for l in open('1988.txt').readlines()]

    def parse(self, response):
        filename = response.url.split("/")[-2]
        open(filename, 'wb').write(response.body)
        item = ImdbcoItem
        for sel in response.xpath('body'):
            item['Followed_by'] = sel.xpath('//*[@id="connections_content"]/div[2]/div[1]/a/text()').extract()
            item['version_of'] = sel.xpath('//*[@id="connections_content"]/div[2]/div[3]/a/text()').extract()
            item['Remade_as'] = sel.xpath('//*[@id="connections_content"]/div[2]/div[4]/a/text()').extract()
        return item

我希望我的输出是这样的:
其次:肮脏的舞蹈,肮脏的舞蹈:哈瓦那之夜
版本:肮脏的舞蹈
重制为:假期
任何帮助都会非常有帮助!

【问题讨论】:

  • 你能用你的parse() 的结果项目更新它吗?不要指望这里的读者总是会运行你的代码(尤其是因为它需要一个本地文件1988.txt)。提供input + code + **actual output** + expected output 可以提出更好的问题,增加获得良好答案的机会。
  • @haben 首先将item = ImdbcoItem 放在for 循环中,不要忘记粘贴() 应该是item = ImdbcoItem()。接下来,您不应该只在return item 中创建yield item 并且仅在for loop 中使用
  • @haben 不要使用/div[2]/div[4] 并且也喜欢这样。

标签: python css xpath web-scraping scrapy


【解决方案1】:

试试这个。我希望它能解决这个问题:

for sel in response.css("div.list"):
    item['Followed_by'] = sel.css("a#followed_by+h4.li_group+div.odd a::text").extract()
    item['version_of'] = sel.css("a#version_of+h4.li_group+div.odd a::text").extract()
    item['Remade_as'] = sel.css("a#remade_as+h4.li_group+div.odd a::text").extract()
return item

如果“Followed by”未能为您提供所有结果,请尝试:

item['Followed_by'] = sel.css("a#followed_by+h4.li_group+div.odd a::text , a#followed_by+h4.li_group+div.odd+div.even a::text").extract()

【讨论】:

  • 它就像一个魅力!谢谢,还有一件事,当我尝试抓取 this 页面时,Followed_by 项仅提取第一和第二个电影标题,但大约有 15 个电影标题。有什么办法可以刮掉所有的电影片名,而不仅仅是第一和第二部?
【解决方案2】:

这可能会有所帮助

    desc = hxs.select('//div[@class = "list"]/text()').extract()
    and then try print(desc[0])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多