【问题标题】:Extract multiple 'p' from within a 'div'从“div”中提取多个“p”
【发布时间】:2022-01-07 18:50:20
【问题描述】:

我希望就我在使用 BeautifulSoup 时遇到的问题获得一些帮助。

import requests
from bs4 import BeautifulSoup

URL = "https://www.baseball-reference.com/players/s/sosasa01.shtml"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.findAll("div",attrs={"class": "p1"})
second_results = soup.findAll("div",attrs={"class": "p2"})
for x in results:
    print(x.find('p').text)
for x in second_results:
    print(x.find('p').text)

在“p1”类中,我试图在所有

photo of HTML I'm trying to scrape

【问题讨论】:

  • x.findAll('p') 的输出是什么?
  • .find() 方法只返回一个结果。也许您打算改用.find_all()
  • @arsho 这是输出:[

    58.6

    ,

    8813

    ,

    2408

    ,

    609

    ,

    .273

    ]
  • 是你要找的吗?

标签: python beautifulsoup


【解决方案1】:

你只有find() 一个<p> 而不是findAll() <p>

for x in results:
    for p in x.findAll('p'):
        print(p.text)
print("--- second")
for x in second_results:
    for p in x.findAll('p'):
        print(p.text)

【讨论】:

  • 非常感谢!非常感谢您的帮助!
猜你喜欢
  • 2021-10-10
  • 2020-09-10
  • 2016-08-12
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 1970-01-01
  • 2011-07-11
相关资源
最近更新 更多