Python 使用BeautifulSoup解析简短字符串
没有办法承认这一点。这个问题出现在这里的唯一原因是一种极端的懒惰,表现为试图避免阅读《圣经》Python 使用BeautifulSoup解析简短字符串,python,beautifulsoup,Python,Beautifulsoup,没有办法承认这一点。这个问题出现在这里的唯一原因是一种极端的懒惰,表现为试图避免阅读《圣经》 import urllib2 response = urllib2.urlopen('http://www.crummy.com/software/BeautifulSoup/bs4/doc/') html = response.read() print len(html) 274574字节的BeautifulSoup文档,仅用于处理您在以下几行中看到的相当简单的href标记 mystr = '<
import urllib2
response = urllib2.urlopen('http://www.crummy.com/software/BeautifulSoup/bs4/doc/')
html = response.read()
print len(html)
274574字节的BeautifulSoup文档,仅用于处理您在以下几行中看到的相当简单的href标记
mystr = '<a href="gazette.html" detail="particularities">Access it!</a>'
soup = BeautifulSoup(mystr, 'html.parser')
print(soup.prettify())
mystr=''
soup=BeautifulSoup(mystr,'html.parser')
打印(soup.prettify())
自然,美化看起来像
<a detail="particularities" href="gazette.html">
Access it!
</a>
现在我想回答以下问题:
mystr
中确实有a
标记吗?并且,在测试后,有:from bs4 import BeautifulSoup
mystr = '<a href="gazette.html" detail="particularities">Access it!</a>'
soup = BeautifulSoup(mystr, 'html.parser')
a = soup.findAll('a', detail='particularities')[0]
print a.text
从bs4导入美化组
mystr=''
soup=BeautifulSoup(mystr,'html.parser')
a=soup.findAll('a',detail='specialities')[0]
打印文本
- (只是因为我是个好人)
- 我们可以按顺序回答您的问题
from bs4 import BeautifulSoup
mystr = '<a href="gazette.html" detail="particularities">Access it!</a>'
soup = BeautifulSoup(mystr, 'html.parser')
a = soup.find('a')
if a: # a will be None if there are no <a> tags
try:
detail = a['detail']
print "Detail: {}".format(detail)
print "Content: {}".format(a.text)
except KeyError: # will throw KeyError if no key named 'detail'
print "No details"
print "Content: {}".format(a.text)
else:
print "No <a> tags"
从bs4导入美化组
mystr=''
soup=BeautifulSoup(mystr,'html.parser')
a=汤。查找('a')
如果a:#如果没有标签,a将为无
尝试:
细节=一个['detail']
打印“详细信息:{}”。格式(详细信息)
打印“内容:{}”。格式(a.text)
除了KeyError:#如果没有名为“detail”的键,则将抛出KeyError
打印“无详细信息”
打印“内容:{}”。格式(a.text)
其他:
打印“无标签”
Ctrl+f组合键确实很神奇:)在BeautifulSoup页面的左侧还有一个不错的导航菜单