[英]How to extract the strong elements which are in div tag
我是网络抓取的新手。 我正在使用 Python 来抓取数据。 有人可以帮助我如何从以下位置提取数据:
<div class="dept"><strong>LENGTH:</strong> 15 credits</div>
我的输出应该是 LENGTH: 15 credits
这是我的代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
length=bsObj.findAll("strong")
for leng in length:
print(leng.text,leng.next_sibling)
输出:
DELIVERY: Campus
LENGTH: 2 years
OFFERED BY: Olin Business School
但我只想有长度。
网站: http : //www.mastersindatascience.org/specialties/business-analytics/
您应该稍微改进代码以通过文本定位strong
元素:
soup.find("strong", text="LENGTH:").next_sibling
或者,对于多个长度:
for length in soup.find_all("strong", text="LENGTH:"):
print(length.next_sibling.strip())
演示:
>>> import requests
>>> from bs4 import BeautifulSoup
>>>
>>> url = "http://www.mastersindatascience.org/specialties/business-analytics/"
>>> response = requests.get(url)
>>> soup = BeautifulSoup(response.content, "html.parser")
>>> for length in soup.find_all("strong", text="LENGTH:"):
... print(length.next_sibling.strip())
...
33 credit hours
15 months
48 Credits
...
12 months
1 year
如果有人还在寻找这个,这里是例子: age = soup.find('div', class_ = 'item-birthday').find('strong').get_text()
这意味着,获取强元素,即在div里面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.