【问题标题】:Website scraping网站抓取
【发布时间】:2017-08-23 01:24:33
【问题描述】:

我正在尝试从此 html 代码中抓取一些数据。更准确地说,我想获得所有数字(这里:[401969217, 401969218])。当然html网站更长,数字也更多。

<td class="nw">1. FC Köln</td>
<td class="nw">Hamburger SV</td>
<td class="nw">3 - 7 - 10</td>
<td class="kicktipp-tippabgabe ">
  <input name="spieltippForms[401969217].tippAbgegeben" id="spieltippForms_401969217_tippAbgegeben" value="true" type="hidden"/>
  <input id="spieltippForms_401969217_heimTipp" name="spieltippForms[401969217].heimTipp" type="tel" value="2" size="2" maxlength="3"/>:
  <input id="spieltippForms_401969217_gastTipp" name="spieltippForms[401969217].gastTipp" type="tel" value="2" size="2" maxlength="3"/>
</td>
</tr>
<tr>
  <td class="nw kicktipp-time">26.08.17 15:30</td>
  <td class="nw">Bayer 04 Leverkusen</td>
  <td class="nw">1899 Hoffenheim</td>
  <td class="nw">6 - 3 - 10</td>
  <td class="kicktipp-tippabgabe ">
    <input name="spieltippForms[401969218].tippAbgegeben" id="spieltippForms_401969218_tippAbgegeben" value="true" type="hidden"/>
    <input id="spieltippForms_401969218_heimTipp" name="spieltippForms[401969218].heimTipp" type="tel" value="2" size="2" maxlength="3"/>:
    <input id="spieltippForms_401969218_gastTipp" name="spieltippForms[401969218].gastTipp" type="tel" value="2" size="2" maxlength="3"/>
  </td>
</tr>
<tr>
  <td class="nw kicktipp-time"/>
  ...

我能够通过以下方法刮掉所有团队:

teams = tree.xpath('//td[@class="nw"]/text()')

不幸的是,我不知道如何修改它来解决我的新问题。希望你能帮忙:)

【问题讨论】:

  • 看到这些数字在输入和 id 名称中,我看不出 lxml 或 xpath 将如何帮助您仅提取数字本身。

标签: python web-scraping lxml


【解决方案1】:

在您的 xpath 表达式中,您不需要 '//td[@class="nw"]/text()',因为它会获取带有 class="nw" 作为属性的标签之间的值。相反,根据您提供的 html 和所需的输出,您应该尝试获取 input 标记的 name 属性并解析该值。

from lxml import html
import re

h = html.fromstring('''<table><tr><td class="kicktipp-tippabgabe ">
  <input name="spieltippForms[401969217].tippAbgegeben" id="spieltippForms_401969217_tippAbgegeben" value="true" type="hidden"/>
  <input id="spieltippForms_401969217_heimTipp" name="spieltippForms[401969217].heimTipp" type="tel" value="2" size="2" maxlength="3"/>:
  <input id="spieltippForms_401969217_gastTipp" name="spieltippForms[401969217].gastTipp" type="tel" value="2" size="2" maxlength="3"/>
</td>
</tr>
<tr>
  <td class="nw kicktipp-time">26.08.17 15:30</td>
  <td class="nw">Bayer 04 Leverkusen</td>
  <td class="nw">1899 Hoffenheim</td>
  <td class="nw">6 - 3 - 10</td>
  <td class="kicktipp-tippabgabe ">
    <input name="spieltippForms[401969218].tippAbgegeben" id="spieltippForms_401969218_tippAbgegeben" value="true" type="hidden"/>
    <input id="spieltippForms_401969218_heimTipp" name="spieltippForms[401969218].heimTipp" type="tel" value="2" size="2" maxlength="3"/>:
    <input id="spieltippForms_401969218_gastTipp" name="spieltippForms[401969218].gastTipp" type="tel" value="2" size="2" maxlength="3"/>
  </td>
</tr>
</table>''')

numbers = [int(x) for e in h.xpath('//input[@type="hidden"]') 
              for x in re.findall(r'\[(\d+)\]', e.get('name'))]

numbers
# returns:
[401969217, 401969218]

【讨论】:

    【解决方案2】:

    另一种获取带有数字的ids 的方法是使用这样的代码。

    >>> from lxml import html
    >>> tree = html.parse('table.htm')
    >>> tree.xpath('.//input[contains(@id,"_heimTipp")]/@id')
    ['spieltippForms_401969217_heimTipp', 'spieltippForms_401969218_heimTipp']
    

    我不知道在ids 的值中会发现什么样的可变性,所以很难说这些应该如何处理。但这可能很简单,

    >>> ids = tree.xpath('.//input[contains(@id,"_heimTipp")]/@id')
    >>> numbers = [int(id.split('_')[1]) for id in ids]
    >>> numbers
    [401969217, 401969218]
    

    【讨论】:

    • 感谢这个额外的解决方案 :) 它真的很容易理解,可能会帮助我解决更多问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 2014-06-18
    相关资源
    最近更新 更多