使用 BeautifulSoup 和 urllib 抓取 <span> 文本 </span>
Scraping <span> text</span> with BeautifulSoup and urllib
我想从 HTML 下面抓取 2015:
我使用以下代码但只能抓取“Annee”
soup.find('span', {'class':'optionLabel'}).get_text()
有人可以帮忙吗?
我是新手
只需尝试找到它的下一个 span
包含您要抓取的文本:
soup.find('span', {'class':'optionLabel'}).find_next('span').get_text()
或css selectors
与adjacent sibling combinator
:
soup.select_one('span.optionLabel + span').get_text()
例子
html='''
<span class="optionLabel"><button>Année</button</span> :
<span>2015</span>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
soup.find('span', {'class':'optionLabel'}).find_next('span').get_text()
输出
2015
我想从 HTML 下面抓取 2015:
我使用以下代码但只能抓取“Annee”
soup.find('span', {'class':'optionLabel'}).get_text()
有人可以帮忙吗?
我是新手
只需尝试找到它的下一个 span
包含您要抓取的文本:
soup.find('span', {'class':'optionLabel'}).find_next('span').get_text()
或css selectors
与adjacent sibling combinator
:
soup.select_one('span.optionLabel + span').get_text()
例子
html='''
<span class="optionLabel"><button>Année</button</span> :
<span>2015</span>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
soup.find('span', {'class':'optionLabel'}).find_next('span').get_text()
输出
2015