lxml网页抓取,特定字词提取

时间:2018-01-29 10:18:04

标签: python web-scraping html-parsing lxml

我正在使用自动化我的脚本从lan-website刮掉计数器并且我现在拉我的头发。

代码看起来像这样

<TR><td><p align="left" style="margin-left: 30;"><b>title</b></p></td><td><p>   </p></td>
</TR>
<TR><td><p align="left" style="margin-left: 40;">table one</p></td><td><p> Task&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;average </p></td>
</TR>
<TR><td><p align="left" style="margin-left: 40;"></p></td><td><p> number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number </p></td>
</TR>
    <TR><td><p align="left" style="margin-left: 40;">1-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 6490&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">2-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 442&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">5-10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">11-20&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">21-30&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;25 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">31-50&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;40 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">sum</p></td><td><p> 6982&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 </p></td>
    </TR>

所以在每个网站我都有相同的单词重复1-2,2-4,5-10等,我想提取数字&#34;在它下面&#34;像6490,442 按特定顺序,它应该看起来像

task - counter
1-1 = 6490
2-4 = 442

这样做我用

import requests
from lxml import html

pageContent=requests.get(
 'http://x.html')
tree = html.fromstring(pageContent.content)
scraped = tree.xpath('//p/text()')
print scraped
女巫显然会打印出这样的东西 \ xa0 \ xa0 \ xa0 \ xa0 \ xa0task&#39;,u&#39; 1-1 \ xa0 \ xa0 \ xa0 \ xa0 \ xa0 \ xa0counter&#39;,u&#39; 6490

我被困的家伙......试图使用其他方法,但我失败了。

2 个答案:

答案 0 :(得分:1)

试试这个。它会获取您上面提到的确切输出。这里content是上面粘贴的html元素的容器。

from lxml.html import fromstring
root = fromstring(content)
for items in root.cssselect("tr")[3:]:
    data = [' '.join(item.text_content().split()).split(" ")[0] for item in items.cssselect("td")]
    print(' = '.join(data))

输出:

1-1 = 6490
2-4 = 442
5-10 = 44
11-20 = 3
21-30 = 2
31-50 = 1
sum = 6982

答案 1 :(得分:0)

这应该可以使它工作。我已经为您的输出提出dict,您可以轻松地将其用于各种目的 -

text = """<TR><td><p align="left" style="margin-left: 30;"><b>title</b></p></td><td><p>   
</p></td>
</TR>
<TR><td><p align="left" style="margin-left: 40;">table one</p></td>
<td><p> Task&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;average </p></td>
</TR>
<TR><td><p align="left" style="margin-left: 40;"></p></td><td><p> number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;number </p></td>
</TR>
    <TR><td><p align="left" style="margin-left: 40;">1-1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 6490&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">2-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 442&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">5-10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">11-20&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">21-30&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;25 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">31-50&nbsp;&nbsp;&nbsp;&nbsp;C</p></td><td><p> 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;40 </p></td>
    </TR>
    <TR><td><p align="left" style="margin-left: 40;">sum</p></td><td><p> 6982&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 </p></td>
    </TR>"""
from bs4 import BeautifulSoup

soup = BeautifulSoup(text, "lxml")
data = {}
for tr in soup.find_all('tr')[3:-1]:
    p = tr.find_all('td')
    task = p[0].text.split()[0].strip()
    counter = p[1].text.split()[0].strip()
    data[task] = counter
print(data)

<强>输出

{'1-1': '6490', '2-4': '442', '5-10': '44', '11-20': '3', '21-30': '2', '31-50': '1'}