Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何提取div标签中的强元素_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 如何提取div标签中的强元素

Python 如何提取div标签中的强元素,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我对网页抓取还不熟悉。我正在使用Python刮取数据。 有人能帮我从以下文件中提取数据吗 <div class="dept"><strong>LENGTH:</strong> 15 credits</div> 输出: DELIVERY: Campus LENGTH: 2 years OFFERED BY: Olin Business School 但我只想要长度 网站:您应该改进您的代码,通过文本定位strong元素: 或者,对于多个长度

我对网页抓取还不熟悉。我正在使用Python刮取数据。 有人能帮我从以下文件中提取数据吗

<div class="dept"><strong>LENGTH:</strong> 15 credits</div>
输出:

DELIVERY:  Campus
LENGTH:  2 years
OFFERED BY:  Olin Business School
但我只想要长度


网站:

您应该改进您的代码,通过文本定位
strong
元素:

或者,对于多个长度:

for length in soup.find_all("strong", text="LENGTH:"):
    print(length.next_sibling.strip())
演示:


如果仍有人在寻找,以下是示例:
age=soup.find('div',class='item birth').find('strong').get_text()
这意味着获取div中的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