beautifulsoup4,正确的使用方法.find_all? [英] beautifulsoup4, correct way to use .find_all?
问题描述
如果我使用解析一个BS4网站,从源头上code我想打印文本26.67%
<字体颜色=绿色>< B>< NOBR> + 26.67%LT; / NOBR>< / B>< / FONT>
我一直与 .find_all()
命令乱搞(的 http://www.crummy.com/software/BeautifulSoup/bs4/doc/ )无济于事。什么是搜索源$ C $ c和打印只是文本的正确方法是什么?
我的code:
进口要求
从BS4进口BeautifulSoup set_url =*插入Web地址在这里*
set_response = requests.get(set_url)
set_data = set_response.text
汤= BeautifulSoup(set_data)
E = soup.find(NOBR)
打印(e.text)
一个小例子:
>>> S =<字体颜色=绿色>< B>< NOBR> + 26.67%LT; / NOBR>< / B>< / FONT>中,
>>>打印■
<字体颜色=绿色>< B>< NOBR> + 26.67%LT; / NOBR>< / B>< / FONT>
>>>从BS4进口BeautifulSoup
>>>汤= BeautifulSoup(S)
>>> E = soup.find(NOBR)
>>> e.text #OR e.get_text()
U'+ 26.67%
找到
返回第一个标签
, find_all
回归一个的ResultSet
:
>>>类型(E)
<类的bs4.element.Tag'>
>>> ES = soup.find_all(NOBR)
>>>类型(ES)
<类的bs4.element.ResultSet'>
>>>为电子商务在ES:
...打印e.get_text()
...
+ 26.67%
如果你想指定的 NOBR
在 B
和字体
,它可以是:
>>> soup.find(字体,{'色':'绿色'})。找到(B)找到(NOBR)get_text()
U'+ 26.67%
连续 .find
可能会导致异常,如果事先 .find
返回None,注意。
If I parse a website using BS4, and from its source code i want to print the text "+26.67%"
<font color="green"><b><nobr>+26.67%</nobr></b></font>
I have been messing around with the .find_all()
command (http://www.crummy.com/software/BeautifulSoup/bs4/doc/) to no avail. What would be the correct way to search the source code and print just the text?
my code:
import requests
from bs4 import BeautifulSoup
set_url = "*insert web address here*"
set_response = requests.get(set_url)
set_data = set_response.text
soup = BeautifulSoup(set_data)
e = soup.find("nobr")
print(e.text)
A small example:
>>> s="""<font color="green"><b><nobr>+26.67%</nobr></b></font>"""
>>> print s
<font color="green"><b><nobr>+26.67%</nobr></b></font>
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(s)
>>> e = soup.find("nobr")
>>> e.text #or e.get_text()
u'+26.67%'
find
return the first Tag
, find_all
return a ResultSet
:
>>> type(e)
<class 'bs4.element.Tag'>
>>> es = soup.find_all("nobr")
>>> type(es)
<class 'bs4.element.ResultSet'>
>>> for e in es:
... print e.get_text()
...
+26.67%
If you want the specified nobr
under b
and font
, it can be:
>>> soup.find("font",{'color':'green'}).find("b").find("nobr").get_text()
u'+26.67%'
Continuous .find
may cause an exception if prior .find
returns None, pay attention.
这篇关于beautifulsoup4,正确的使用方法.find_all?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!