Extracting tag from bs4.element.tag returns empty string Extracting tag from bs4.element.tag returns empty string json json

Extracting tag from bs4.element.tag returns empty string


You have to use .string to get the object.

Here's how:

import jsonimport requestsfrom bs4 import BeautifulSoupsoup = 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"])

For example, this prints:

12

To print the answers use this:

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


You need to call the .string method:

import jsonimport requestsfrom bs4 import BeautifulSoupurl = '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"})json_data = json.loads(answers.string)
>>> print(type(json_data))<class 'dict'>