我已经想出了如何从 中提取文本itemprop
但我无法从中提取文本<div clas="someclass">Extract This Text Here!</div>
我只粘贴了我的代码中不起作用的部分,但如果需要,将粘贴整个内容。
我已经用 BeautifulSoup 和 Python 设置了一个变量来获取页面,但它不会只抓取文本。
编辑:有些文本包含在h1
标签中,有些文本位于p
具有多个跨度的标签中。
编辑 2:所以一些数据在 a 中<div class=“someclass”><h1>There’s the text</h1></div>
,另一个在<p class=“anotherclass”><span>This is another text</span></p>
. 如何从多个标签中提取文本?
for each_business in info:
yp_bus_url = each_business.find('a', {'class': 'business-name'}).get('href')
whole_url = "https://www.yellowpages.com"+yp_bus_url
print(whole_url)
bus_page = requests.get(whole_url)
bus_soup = BeautifulSoup(page.text, 'html.parser')
# The variable below wont get text. I've tried different variations with it too but it doesn't work.
business_name = bus_soup.findAll("div", class_="sales-info")
print(business_name)
我已经使用您在问题中给出的 html 来提取里面的文本<p>
和<div>
标签。我希望这就是你要找的
html='''<div class="someclass"><h1>There’s the text</h1></div><p class="anotherclass"><span>This is another text</span>'''
soup = BeautifulSoup(html,'lxml')
print(soup.find('div',class_='someclass').text)
print(soup.find('p',class_='anotherclass').text)
输出这是
文本
这是另一个文本
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句