嘿,我正在做一个Python项目,这个项目需要我浏览网页。我希望通过查找来查找特定的文本,如果它找到了该文本,则它会打印出一些内容。如果没有,它会打印出一条错误消息。我已经尝试过不同的模块,比如libxml,但是我不知道该怎么做。
有人能帮上忙吗?
发布于 2011-02-08 04:13:18
你可以做一些简单的事情,比如:
import urllib2
import re
html_content = urllib2.urlopen('http://www.domain.com').read()
matches = re.findall('regex of string to find', html_content);
if len(matches) == 0:
print 'I did not find anything'
else:
print 'My string is in the html'发布于 2011-02-08 04:16:38
lxml非常棒:http://lxml.de/parsing.html
我经常在xpath中使用它来从html中提取数据。
另一个选择是http://www.crummy.com/software/BeautifulSoup/,它也很棒。
https://stackoverflow.com/questions/4925966
复制相似问题