0
我正在用lxml在Python中刮取網頁,並試圖獲取名爲(Table3)的表下的文本。在下面的代碼中你可以看到在下面的代碼中tr的數字,然後在每個tr下面有4個td。使用xpath颳去網頁,返回結果文本
我想要的是在列表中的所有tr下打印td1的文本。
這裏的HTML代碼:
<table width="100%" cellspacing="1" cellpadding="0" border="0" class="Table3">
<TBODY>
<TR>
<Th class="calibri-12" align="center">Symbol</Th>
<Th class="calibri-12" align="center">CompanyName</Th>
<Th class="calibri-12" align="center">Short Name</Th>
<Th class="calibri-12" align="center">ISIN Code</Th>
</TR>
<TR>
<TD >1330</TD>
<TD >ALKHODARI</TD>
<TD >SA12L0O0KP12</TD>
</TR>
<TR>
<TD >4001</TD>
<TD >A.Othaim Market</TD>
<TD >SA1230K1UGH7</TD>
</TR>
<TR>
<TD >1820</TD>
<TD >Al Hokair Group</TD>
<TD >SA13IG50SE12</TD>
</TR>
,我這裏使用的代碼:
from lxml import html
import requests
page = requests.get('http://www.example.com')
tree = html.fromstring(page.content)
code_test = tree.xpath('//table[@class = "Table3"]//td[1]')
print code_test
,結果是這樣的:
<Element td at 0x7f4e7bbf5b50>, <Element td at 0x7f4e7bbf5ba8>, <Element td at 0x7f4e7bbf5c00>, <Element td at 0x7f4e7bbf5c58>, <Element td at 0x7f4e7bbf5cb0>, <Element td at 0x7f4e7bbf5d08>, <Element td at 0x7f4e7bbf5d60>, <Element td at 0x7f4e7bbf5db8>, <Element td at 0x7f4e7bbf5e10>, <Element td at 0x7f4e7bbf5e68>, <Element td at 0x7f4e7bbf5ec0>, <Element td at 0x7f4e7bbf5f18>, <Element td at 0x7f4e7bbf5f70>, <Element td at 0x7f4e7bbf5fc8>, <Element td at 0x7f4e7bbf6050>, <Element td at 0x7f4e7bbf60a8>, <Element td at 0x7f4e7bbf6100>, <Element td at 0x7f4e7bbf6158>, <Element td at 0x7f4e7bbf61b0>, <Element td at 0x7f4e7bbf6208>]
非常感謝,它像一個魅力... –