如何使用Beautifutdsoup在Python中找到特定的课程

发布于 2025-02-13 06:50:43 字数 873 浏览 1 评论 0原文

来自< e节</span> land</h2>我只是在提取class =“ H1”因为我需要在此< span ID =“ Ref613779”之间的字符串问题是,它没有给我所有出现的结果class =“ h1”它只是为我首次出现的结果提供了结果class =“ H1” class ='h1 in < e节h1“>< span id =“ Ref613784”></span>/h2> 这里网站链接https://www.britannica.com/place/alabama-state我尝试从该网站(htmlcontent)中提取该字符串,超过两个出现class = 'H1,但我只是从该网站给您两个示例。
我的代码:

from bs4 import BeautifulSoup
import requests
htmlrequests=requests.get('https://www.britannica.com/place/Alabama-state')
htmlcontent=htmlrequests.content
soup=BeautifulSoup(htmlcontent,'html.parser')
for section in soup.find_all(class_='h1'):
    print(section.text)

输出是:land和预期land 足以解释我的问题,等待您的解决方案吗

From <section id="ref78299" data-level="1" data-has-spy="true"><h2 class="h1"><span id="ref613779"></span>Land</h2> I am just extracting class="h1" because I need string between this <span id="ref613779"></span>Land</h2> and its working but the problem is that its not giving me results for all occurrence class="h1" its just gave me a result for a first occurrence of class="h1"
Hower occurrence of class='h1 in <section id="ref273744" data-level="1" data-has-spy="true"><h2 class="h1"><span id="ref613784"></span>People</h2>
Here website link https://www.britannica.com/place/Alabama-state from which I am trying to extract that string at that website(htmlcontent) more than two occurrence of class='h1 but I am just giving you a example of two from that website.
My code:

from bs4 import BeautifulSoup
import requests
htmlrequests=requests.get('https://www.britannica.com/place/Alabama-state')
htmlcontent=htmlrequests.content
soup=BeautifulSoup(htmlcontent,'html.parser')
for section in soup.find_all(class_='h1'):
    print(section.text)

and output is:Land and expected Land People
is it enough to explain my problem ,waiting for your solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻许诺言 2025-02-20 06:50:43

这是你想要的吗?

# Python program to print all heading tags
import requests
from bs4 import BeautifulSoup
 
# scraping a wikipedia article
url_link = 'https://www.britannica.com/place/Alabama-state'
request = requests.get(url_link)
 
Soup = BeautifulSoup(request.text, 'lxml')
 
# creating a list of all common heading tags
heading_tags = ["h1", "h2", "h3"]
for tags in Soup.find_all(heading_tags):
    print(tags.name + ' -> ' + tags.text.strip())

结果:

在此处输入图像描述“

Is this what you want?

# Python program to print all heading tags
import requests
from bs4 import BeautifulSoup
 
# scraping a wikipedia article
url_link = 'https://www.britannica.com/place/Alabama-state'
request = requests.get(url_link)
 
Soup = BeautifulSoup(request.text, 'lxml')
 
# creating a list of all common heading tags
heading_tags = ["h1", "h2", "h3"]
for tags in Soup.find_all(heading_tags):
    print(tags.name + ' -> ' + tags.text.strip())

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文