Ronique Hossain Ronique Hossain - 6 months ago 24
Python Question

Parsing a JSON file with BeautifulSoup

Fairly new to coding so bear with me for any fundamental mistakes. Simplified, I have this:

url = "https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgIuEiRWTFBMbFRwZUhIS2o1TFNpRVpKbXJxZGg0MHlqZjBmdG4xak4aBkNHVSUzRA%253D%253D"

lm_json = requests.get(url).json()


How would I go about parsing
lm_json
with BeautifulSoup? I keep getting errors to open it as a file. I need to use BeautifulSoup because I'm using the result in a while loop.

From this step, I'd need to append it to the main soup.

Thanks for any help!

Answer

The HTML content is wrapped in a JSON. For your example, you can try:

url = "https://www.youtube.com/browse_ajax?action_continuation=1&continuation=4qmFsgIuEiRWTFBMbFRwZUhIS2o1TFNpRVpKbXJxZGg0MHlqZjBmdG4xak4aBkNHVSUzRA%253D%253D"
lm_json = requests.get(url).json()
soup = BeautifulSoup(lm_json["content_html"])
Comments