【问题标题】:First time using scrapy, trying to crawl a set of tables第一次使用scrapy,尝试爬取一组表
【发布时间】:2017-01-04 20:20:52
【问题描述】:

我对 Python 非常陌生(对 stackoverflow 也很陌生),并且刚刚开始使用 Scrapy。我希望从不同的网站获取一些爱好产品信息。我已经阅读了教程,感觉很迷茫。我想要的是表中列出的手表的属性,但它们在第二个表中都有相同的类(“productTitle”)。

<table border="0" cellspacing="0" cellpadding="4">
  <tbody>
    <tr>
      <td class="productTitle creditCardPrice" valign="top">
        <strong>Regular Price:</strong>
      </td> 
      <td valign="top">$9,072</td>
    </tr>
    <tr>
      <td class="productTitle retailPrice" valign="top">
        <strong>Retail Price:</strong>
      </td> 
      <td valign="top">$12,350</td> 
    </tr>
    <tr>
      <td class="productTitle itemNumber" valign="top">
        <strong>Item Number:</strong>
      </td> 
      <td valign="top">112555</td> 
    </tr>
  </tbody>
</table>

第二张桌子:

<table border="0" cellpadding="4" cellspacing="0">
  <tbody>
    <tr style="height: 15px;">
      <td class="productTitle" style="height: 15px;" valign="top"> .     
        <strong>Manufacturer:</strong>
      </td> 
      <td style="height: 15px;" valign="top">Rolex</td> 
    </tr>
    <tr style="height: 30px;">
      <td class="productTitle" style="height: 30px;" valign="top">
        <strong>Model Name/Number:</strong>
      </td> 
      <td style="height: 30px;" valign="top">Yacht-Master 116622</td> 
    </tr>

还有更多的数据行。您可以在此处查看示例:https://www.bobswatches.com/rolex-platinum-yacht-master-116622-pre-owned.html

我的目标是将所有这些数据放入一个 .csv 文件中,每列都标有“信用卡价格”、“制造商”、“型号名称/编号”等,然后从网站上抓取我最喜欢的手表并为每只手表创建一张包含所有这些细节的表格。但是,在我到达蜘蛛在不同页面中移动的部分之前,我必须让它正确地抓取这一页。

我不知道如何使用 Scrapy 写出来。我正在跳到其他几个 stackoverflow 问题并仍在玩教程,但进展非常缓慢。这显然是错误的,但我在哪里:

    def parse(self, response):
    for row in response.selector.xpath('//table'):
        yield {
            'text': row.xpath('./td[1]').extract_first(),
        }

    next_page_url = response.xpath('//li[@class="next"]/a/@href').extract_first()
    if next_page_url is not None:
        yield scrapy.Request(response.urljoin(next_page_url))

【问题讨论】:

  • 那有什么问题?
  • @eLRuLL 添加更多内容以明确这一点
  • 我还是不明白。 “来自不同网站的爱好产品信息”是什么意思。如果您无法正确格式化表格,请尝试创建一个具有类似结构的更简单示例。还请指定您要获取的 html 的哪些部分。
  • 增加了更多的清晰度。这有帮助吗?

标签: python xpath scrapy web-crawler


【解决方案1】:

如果我理解正确,您想提取结构化数据:从该表中提取行标题和行数据?

您可以通过以下方式实现:

  1. 为每一行提取所有行
  2. 为每一行提取一个标题和行数据

所以这只是使用正确的xpath 选择器的问题。例如,这样的事情可以解决问题:

# find all table rows
rows = response.xpath("//tr")
for row in rows:
    title = row.xpath(".//strong/text()").extract_first()
    text = ''.join(row.xpath(".//td/text()").extract()).strip('. \n')
    print(title)
    print(text)
    print('-'*80)

返回:

Regular Price:
$9,072
--------------------------------------------------------------------------------
Retail Price:
$12,350
--------------------------------------------------------------------------------
Item Number:
112555
--------------------------------------------------------------------------------
Regular Price:
$9,072
--------------------------------------------------------------------------------
Retail Price:
$12,350
--------------------------------------------------------------------------------
Item Number:
112555
--------------------------------------------------------------------------------
Manufacturer:
Rolex
--------------------------------------------------------------------------------
Model Name/Number:
Yacht-Master 116622
--------------------------------------------------------------------------------

【讨论】:

    猜你喜欢
    • 2012-12-22
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多