【发布时间】:2010-03-01 10:41:10
【问题描述】:
我想从一个 url(链接)中提取一个字符串。该字符串位于<h3></h3> 标记中。
link = http://www.test.com/page.html
Content of link: <h3>Text here</h3>
首先获取 page.html 的内容/源代码然后提取链接的优雅方法是什么? 谢谢!
【问题讨论】:
我想从一个 url(链接)中提取一个字符串。该字符串位于<h3></h3> 标记中。
link = http://www.test.com/page.html
Content of link: <h3>Text here</h3>
首先获取 page.html 的内容/源代码然后提取链接的优雅方法是什么? 谢谢!
【问题讨论】:
我推荐Beatiful Soup。这是一个很好的 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]
【讨论】:
您可以使用 URLLib2 来检索 URL 的内容:
http://docs.python.org/library/urllib2.html
然后您可以使用 Python 库中的 HTML 解析器来查找正确的内容:
【讨论】:
如果你想要的文本是页面上的唯一 <h3>-wrapped文本,试试:
from urllib2 import urlopenfrom re import searchtext = search(r'(?<=<h3>).+?(?=</h3>)', urlopen(link).read()).group(0)
如果有多个<h3>-wrapped 字符串,您可以在模式中添加更多细节或使用re.finditer()/re.findall()
【讨论】: