怎么做?
如何使用python或php获取带有已加载的html页面的嵌套标签?
您能给我一个可以学习的网站吗?
from HTMLParser import HTMLParser
import urllib
class MyHTMLParser(HTMLParser):
def handlestarttag(self, tag, attrs):
print "Poczatek %s" % tag
def handleendtag(self, tag):
print "Koniec %s tag" % tag
def handledata(self, data):
print "Dane %s" % data
p = MyHTMLParser()
input = urllib.urlopen('url')
html = input.read()
input.close()
p.feed(html)
这是给你的一个例子:
from bs4 import BeautifulSoup
# Use urlopen to read web pages, this is only an e
test_input = r'<html><body><div id="bar"><p>Foo</p></div></body></html>'
soup = BeautifulSoup(test_input)
print soup.find('div', {'id': 'bar'}).p.text
这将产生:
Foo
查看BS的文档以获取更多示例。这里重要的是要使用现有的库,而不要尝试自己创建一个库。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句