【问题标题】:Scrape Table Using Scrapy使用 Scrapy 刮表
【发布时间】:2020-07-28 19:35:43
【问题描述】:

为长篇道歉 -

我有一张表,我正在尝试使用 scrapy 进行挖掘,但不知道如何深入挖掘该表。

这是桌子:

<table class="detail-table" border="0" cellspacing="0">
 <tbody>
 <tr id="trAnimalID">
  ...
 </tr>
 <tr id="trSpecies">
  ...
 </tr>
 <tr id="trBreed">
  ...
 </tr>
 <tr id="trAge">
  ...
 <tr id="trSex">
  ...
 </tr>
 <tr id="trSize">
  ...
 </tr>
 <tr id="trColor">
  ...
 </tr>
 <tr id="trDeclawed">
  ...
 </tr>
 <tr id="trHousetrained">
  ...
 </tr>
 <tr id="trLocation">
  ...
 </tr>
 <tr id="trIntakeDate">
  <td class="detail-label" align="right">
   <b>Intake Date</b>
  </td>
  <td class="detail-value">
   <span id="lblIntakeDate">3/31/2020</span>&nbsp;
  </td>
 </tr>
 <tr id="trStage">
  <td class="detail-label" align="right">
   <b>Stage</b>
  </td>
  <td class="detail-value">
   <span id="lblStage">Reserved</span>
  </td>
 </tr>
 </tbody></table>

我可以使用scrapy shell 命令深入了解它:

text = response.xpath('//*[@class="detail-table"]//tr')[10].extract()

我要回来了:

'<tr id="trIntakeDate">\r\n\t
  <td class="detail-label" align="right">\r\n
   <b>Intake Date</b>\r\n
  </td>\r\n\t
  <td class="detail-value">\r\n
   <span id="lblIntakeDate">3/31/2020</span>\xa0\r\n
  </td>\r\n
</tr>'

我不太清楚如何获取 lblIntakeDate 的值。我只需要 2020 年 3 月 31 日。此外,我想将其作为 lambda 运行,并且无法完全弄清楚如何让执行函数转储出一个 json 文件,就像我可以使用命令行一样。有什么想法吗?

【问题讨论】:

    标签: python html-table scrapy


    【解决方案1】:

    试试看:

    //table[@class='detail-table']/tbody//tr/td/span[@id='lblIntakeDate']/text()
    

    https://www.online-toolz.com/tools/xpath-tester-online.php 并且请去掉&amp;nbsp;等冗余字符

    【讨论】:

    • 这把我拉向了正确的方向。您发布的查询并没有拉回任何东西,但我在 text() 中切片并且它有效!这就是我能够得到的工作:text = response.xpath('//*[@id="lblIntakeDate"]/text()').extract() 感谢您的帮助和链接该查询编辑器!
    • @MikeSimpson 不客气,我很高兴能帮上忙,祝你好运。
    【解决方案2】:

    尝试:

    from urllib.request import urlopen
    
    url = ''
    html = urlopen(url)
    bs = BeautifulSoup(html.read(), 'html.parser')
    
    for i in bs.find_all('a'):
        print(i.get_text())
    

    【讨论】:

      猜你喜欢
      • 2014-07-16
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多