0

The target is to extract a MP4 video link on MLB website.

url ="https://www.mlb.com/video/jeremy-pena-s-solo-homer?t=most-popular"
content = requests.get(url).text

I have found the target dict.

soup = BeautifulSoup(content,"lxml")

all_script_label = soup.find_all(name ="script")

target = all_script_label[20].text.split("\n")[1].split("=")[1]

But I can't turn the target into dict type with json.loads, it's still a string.

json_ob = json.loads(target)
print(type(json_ob))

Which step I did wrong?

I have tried ast.literal_eval method but it doesn't work too.

3
  • What is target? Commented Nov 5, 2022 at 11:09
  • Target is the dictionary which contains the video link Commented Nov 5, 2022 at 13:24
  • Can you update your question with a relevant sample from target? Commented Nov 5, 2022 at 13:26

1 Answer 1

0

You can apply json.loads second time to convert the str to dict:

import re
import json
import requests
from bs4 import BeautifulSoup

url = "https://www.mlb.com/video/jeremy-pena-s-solo-homer?t=most-popular"
content = requests.get(url).text
soup = BeautifulSoup(content, "lxml")
all_script_label = soup.find_all(name="script")
target = all_script_label[20].text


data = re.search(r"window\.__VIDEO_INIT_STATE__ = (.*)", target).group(1)
data = json.loads(json.loads(data))

print(type(data))

Prints:

<class 'dict'>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.