【发布时间】:2011-05-16 18:13:37
【问题描述】:
有人可以帮我解析一个html文件以获取python文件中所有图像的链接吗?
最好没有第 3 方模块...
谢谢!
【问题讨论】:
有人可以帮我解析一个html文件以获取python文件中所有图像的链接吗?
最好没有第 3 方模块...
谢谢!
【问题讨论】:
您可以使用Beautiful Soup。我知道你说没有第 3 方模块。但是,这是解析 HTML 的理想工具。
import urllib2
from BeautifulSoup import BeautifulSoup
page = BeautifulSoup(urllib2.urlopen("http://www.url.com"))
page.findAll('img')
【讨论】:
BeautifulSoup(page)
仅使用 PSL
from html.parser import HTMLParser
class MyParse(HTMLParser):
def handle_starttag(self, tag, attrs):
if tag=="img":
print(dict(attrs)["src"])
h=MyParse()
page=open("index.html").read()
h.feed(page)
【讨论】:
普遍认为 lxml 比 Beautiful Soup (ref) 更快。它的教程可以在这里找到:(link) 你也可以看看this old stackoverflow post。
【讨论】: