如何从< p>刮擦Wikipedia文本没有ID或课程?

发布于 2025-01-27 00:40:54 字数 490 浏览 4 评论 0原文

我正在抓取Wikipedia文本,但是< p>没有任何类或ID:

import requests as r
from bs4 import BeautifulSoup as bs

url=r.get("https://en.wikipedia.org/wiki/Wikipedia#Nupedia")
soup=bs(url.text,'html.parser')
print(soup)

product=soup.find('div',class_="mw-parser-output")
head=product.find('span',id="Nupedia").text
para=product.find_all('p',class_=False)
print(para)

它不起作用

“

I am scraping a Wikipedia text but the <p> does not have any class or id:

import requests as r
from bs4 import BeautifulSoup as bs

url=r.get("https://en.wikipedia.org/wiki/Wikipedia#Nupedia")
soup=bs(url.text,'html.parser')
print(soup)

product=soup.find('div',class_="mw-parser-output")
head=product.find('span',id="Nupedia").text
para=product.find_all('p',class_=False)
print(para)

It's not working

1

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

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

发布评论

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

评论(2

若沐 2025-02-03 00:40:54

问题不是很清楚 - 仅获取下一个&lt; p&gt;您可以接受:

product.find('span',id="Nupedia").find_next('p').text

如果您想提取头条新闻及其相应的&lt; p&gt; s可以使用类似的东西:

for h in soup.select('h3'):
    print(h.text)
    for t in h.next_siblings:
        if t.name == 'h3':
            break
        if t.name == 'p':
            print(t.text)

Question is not quiet clear - To get only the next <p> you could go with:

product.find('span',id="Nupedia").find_next('p').text

If you like to extract the headlines and its corresponding <p>s you can go with something like that:

for h in soup.select('h3'):
    print(h.text)
    for t in h.next_siblings:
        if t.name == 'h3':
            break
        if t.name == 'p':
            print(t.text)
心碎的声音 2025-02-03 00:40:54

要从p标签获取文本,您可以使用.find_next_sibling('p')

import requests as r
from bs4 import BeautifulSoup as bs
url=r.get("https://en.wikipedia.org/wiki/Wikipedia#Nupedia")
soup=bs(url.text,'html.parser')
#print(soup)
product=soup.find('div',class_="mw-parser-output")
head=product.find('span',id="Nupedia")
para=product.find('div',class_="thumb tright").find_next_sibling('p').get_text(strip=True)
print(para)

output:

Other collaborative online encyclopedias were attempted before Wikipedia, but none were as successful.[17]Wikipedia began as a complementary project forNupedia, a free online English-language encyclopedia project whose articles were written by experts and reviewed under a formal process.[18]It was founded on March 9, 2000, under the ownership ofBomis, aweb portalcompany. Its main figures were Bomis CEOJimmy WalesandLarry Sanger,editor-in-chieffor Nupedia and later Wikipedia.[1][19]Nupedia was initially licensed under its own NupediaOpen ContentLicense, but even before Wikipedia was founded, Nupedia switched to theGNU Free Documentation Licenseat the 
urging ofRichard Stallman.[20]Wales is credited with defining the goal of making a publicly editable encyclopedia,[21][22]while Sanger is credited with the strategy of using awikito reach 
that goal.[23]On January 10, 2001, Sanger proposed on the Nupedia mailing list to create a wiki as a "feeder" project for Nupedia.[24]

To get text from p tag, you can use .find_next_sibling('p')

import requests as r
from bs4 import BeautifulSoup as bs
url=r.get("https://en.wikipedia.org/wiki/Wikipedia#Nupedia")
soup=bs(url.text,'html.parser')
#print(soup)
product=soup.find('div',class_="mw-parser-output")
head=product.find('span',id="Nupedia")
para=product.find('div',class_="thumb tright").find_next_sibling('p').get_text(strip=True)
print(para)

Output:

Other collaborative online encyclopedias were attempted before Wikipedia, but none were as successful.[17]Wikipedia began as a complementary project forNupedia, a free online English-language encyclopedia project whose articles were written by experts and reviewed under a formal process.[18]It was founded on March 9, 2000, under the ownership ofBomis, aweb portalcompany. Its main figures were Bomis CEOJimmy WalesandLarry Sanger,editor-in-chieffor Nupedia and later Wikipedia.[1][19]Nupedia was initially licensed under its own NupediaOpen ContentLicense, but even before Wikipedia was founded, Nupedia switched to theGNU Free Documentation Licenseat the 
urging ofRichard Stallman.[20]Wales is credited with defining the goal of making a publicly editable encyclopedia,[21][22]while Sanger is credited with the strategy of using awikito reach 
that goal.[23]On January 10, 2001, Sanger proposed on the Nupedia mailing list to create a wiki as a "feeder" project for Nupedia.[24]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文