我正在尝试解析网站上的数据。例如,对于我试图从中提取数据的站点,SRC代码的部分看起来像这样。
<table summary="Customer Pending and Vendor Pending Table">
<tr>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=Level&Escalationorder=0#Escalation" class="headlink">
<img src="/images/rat/up_selected.png" width="11" height="9" border="0" alt="up">Risk </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=AvgLastUpd&Escalationorder=1#Escalation" class="headlink">
Avg Last Updated </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=AvgDaysOpen&Escalationorder=1#Escalation" class="headlink">
Avg Days Open </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=Srs&Escalationorder=1#Escalation" class="headlink">
# of Cases </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort_pct=1&Escalationorder=1#Escalation" class="headlink">% of Total Cases</a> </th>
</tr>
<tr >
<td><a href="/snapshot.php?statusrisk=2&wrkgrp=Somedata&function=statusrisk&statuses=CustomerPending"><img src="/images/rat/severity_2.gif" alt="Very High Risk" title="Very High Risk" border="0"></a></td>
<td> 8.0</td>
<td> 69.0</td>
<td>1</td>
<td> 3.1</td>
</tr>
我需要从上表中提取值8.0,69.0和3.1。我的Python代码看起来像这样。
from lxml import html
import requests
page = requests.get('http://rat-sucker.abc.com/team.php?wrkgrp=somedata')
tree = html.fromstring(page.text)
Stats = tree.xpath(//*[@id="leftrat"]/table[1]/tbody/tr[2]/td[2])
print 'Stats: ', Stats
我已经使用多种方法和Xcode模拟器检查了我的Xpath,它是正确的(如果你运行上面的部分代码它可能不起作用),但是当我的python脚本运行时它不会产生任何输出。
[root @ testbed testhost] #python scrapper.py 统计
[root @ testbed testhost]#
答案 0 :(得分:4)
您可以使用BeautifulSoup parser。
>>> s = '''<table summary="Customer Pending and Vendor Pending Table">
<tr>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=Level&Escalationorder=0#Escalation" class="headlink">
<img src="/images/rat/up_selected.png" width="11" height="9" border="0" alt="up">Risk </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=AvgLastUpd&Escalationorder=1#Escalation" class="headlink">
Avg Last Updated </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=AvgDaysOpen&Escalationorder=1#Escalation" class="headlink">
Avg Days Open </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort=Srs&Escalationorder=1#Escalation" class="headlink">
# of Cases </a> </th>
<th> <a href="/team.php?wrkgrp=Somedata&Escalationsort_pct=1&Escalationorder=1#Escalation" class="headlink">% of Total Cases</a> </th>
</tr>
<tr >
<td><a href="/snapshot.php?statusrisk=2&wrkgrp=Somedata&function=statusrisk&statuses=CustomerPending"><img src="/images/rat/severity_2.gif" alt="Very High Risk" title="Very High Risk" border="0"></a></td>
<td> 8.0</td>
<td> 69.0</td>
<td>1</td>
<td> 3.1</td>
</tr>'''
>>> soup = BeautifulSoup(s)
>>> [i.text.strip() for i in soup.find_all('td', text=True)]
['8.0', '69.0', '1', '3.1']