gpt4 book ai didi

python - 从 bs4.element.tag 中提取标签返回空字符串

转载 作者:行者123 更新时间:2023-12-04 15:13:04 26 4
gpt4 key购买 nike

我正在尝试按照教程从 Quora 网址中提取所有答案。我的代码看起来像这样

url = 'https://www.quora.com/Should-I-move-to-London'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
answers = soup.find("script", {"type": "application/ld+json"})
answers

但是,当我尝试从答案(bs4.element.tag 对象)中获取文本时,它只是显示为空。我怎样才能提取所有的答案?我还尝试了以下

data = json.loads(soup.find('script', type='application/ld+json').text)

但我收到以下错误

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我附上了一张带有 bs4 body 结构的截图。 enter image description here

最佳答案

您必须使用 .string 来获取对象。

方法如下:

import json

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get('https://www.quora.com/Should-I-move-to-London').content, 'html.parser')
answers = soup.find("script", {"type": "application/ld+json"})
data = json.loads(answers.string)
print(data["mainEntity"]["answerCount"])

例如,这会打印:

12

要打印答案,请使用:

for number, answer in enumerate(data["mainEntity"]["suggestedAnswer"], start=1):
print(f"Answer: {number}. | Upvote count: {answer['upvoteCount']}")
print(answer["text"].strip())
print("-" * 80)

关于python - 从 bs4.element.tag 中提取标签返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64861501/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com