我正在尝试制作一个网络爬虫,它将解析出版物的网页并提取作者。网页的骨架结构如下:
<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)
我意识到很多导入语句可能是多余的,但我只是复制了我目前在更多源文件中的任何内容。
编辑:我想我并没有说得很清楚,但是我在页面中有多个要抓取的标签。