[英]python parsing url after string
我想从网址(链接)中提取一个字符串。 该字符串在<h3></h3>
标记中。
link = http://www.test.com/page.html
Content of link: <h3>Text here</h3>
首先获取page.html的内容/源代码然后提取链接的一种优雅方法是什么? 谢谢!
我会推荐美丽汤 。 这是用于HTML页面已损坏的很好的解析器(在大多数情况下,您不必担心页面格式不正确)。
您可以使用URLLib2来检索URL的内容:
http://docs.python.org/library/urllib2.html
然后,您可以使用Python库中的HTML解析器来找到正确的内容:
import urllib2
url="http://www.test.com/page.html"
page=urllib2.urlopen(url)
data=page.read()
for item in data.split("</h3>"):
if "<h3>" in item:
print item.split("<h3>")[1]
如果您想要的文本是页面上唯一的 <h3>
换行文本,请尝试:
from urllib2 import urlopen
from re import search
text = search(r'(?<=<h3>).+?(?=</h3>)', urlopen(link).read()).group(0)
如果有多个<h3>
包装的字符串,则可以将更多详细信息放入模式中,也可以使用re.finditer()
/ re.findall()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.