我正在試圖製作一個網絡抓取工具,它將解析出版物的網頁並提取作者。該網頁的骨骼結構如下:涉及具有屬性的HTML標記的Python網絡抓取
<html>
<body>
<div id="container">
<div id="contents">
<table>
<tbody>
<tr>
<td class="author">####I want whatever is located here ###</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
我一直在嘗試使用BeautifulSoup和LXML迄今完成這一任務,但我不知道如何處理這兩個div標籤和td標籤,因爲它們具有屬性。除此之外,我不確定是否應該更多地依賴BeautifulSoup或lxml或兩者的組合。我該怎麼辦?
此刻,我的代碼看起來像下面的是:
import re
import urllib2,sys
import lxml
from lxml import etree
from lxml.html.soupparser import fromstring
from lxml.etree import tostring
from lxml.cssselect import CSSSelector
from BeautifulSoup import BeautifulSoup, NavigableString
address='http://www.example.com/'
html = urllib2.urlopen(address).read()
soup = BeautifulSoup(html)
html=soup.prettify()
html=html.replace(' ', ' ')
html=html.replace('í','í')
root=fromstring(html)
我知道很多import語句可能是多餘的,但我只是複製任何我目前有更多的源文件。
編輯:我想,我沒有這樣做很清楚,但我有多個標籤頁,我想刮。
謝謝,亞歷克斯。我在頁面上有多個作者,所以我將擁有多個td標籤。我如何迭代它們中的每一個? – GobiasKoffi 2009-09-08 03:21:42