【问题标题】:Python scraping with Selenium and Beautifulsoup can't extract nested tag, error object is not callable使用 Selenium 和 Beautifulsoup 进行 Python 抓取无法提取嵌套标签,错误对象不可调用
【发布时间】:2020-09-16 02:57:06
【问题描述】:

我无法使用 find_next、find_children 或 find_next_siblings 提取嵌套标记,导致 TypeError: can only concatenate str(not"Tag") to str 或 object is not callable,有什么方法可以提取嵌套标记?谢谢。

【问题讨论】:

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

BeautifulSoup documentation 中没有这样的方法find_children,这就是为什么您会收到object is not callable 错误。但是,您可以使用 contents[0] 提取 weight 和 weight_scale,而不必依赖循环,因为这是一个固定的网页 html,不会很快改变。

请注意,我选择使用 requests 库向网页发出请求并获取 HTML,但您可以尝试使用 PhantomJS 运行您的代码。

import requests
from bs4 import BeautifulSoup

page = requests.get("https://stats.nba.com/player/1629121/")
soup = BeautifulSoup(page.content, 'html.parser')

height_div = soup.find('div', string='HT')
height = height_div.find_next().contents[0]

weight_div = soup.find('div', string='WT')
weight = weight_div.find_next().contents[0].strip()
weight_scale = weight_div.find_next().contents[1].contents[0]

age_div = soup.find('div', string='AGE')
age = age_div.find_next().contents[0]

print(height)
print(f"{weight} {weight_scale}")
print(age)

如果你还想尝试其他方法,可以查看我之前链接的文档。

【讨论】:

  • @S.Y 也请查看@akane 评论的链接。我必须浏览图片,而不仅仅是复制您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
相关资源
最近更新 更多