V
V
Vladimir Vladimirovich2020-07-13 12:28:45
Python
Vladimir Vladimirovich, 2020-07-13 12:28:45

How to get result from script tag?

Hello!
I wanted to parse the link from the video .
The link is in the script, having searched on the Internet that it can be pulled out from there using the json library, and the .loads () method, respectively.
But for some reason an error occurs:
raise JSONDecodeError("Expecting value", s, err.value) from None

json.decoder.JSONDecodeError : Expecting value: line 1 column 1 (char 0) if you have free time and desire.
I am attaching the code:

import requests
import json
from bs4 import BeautifulSoup

url = 'https://www.tiktok.com/@golden_men_6/video/6803237854542712070'

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}


r = requests.get(url, headers=headers)

soup = BeautifulSoup(r.content, 'html.parser')
script = soup.find('script', attrs={'id': 'videoObject'})

data = json.loads(script.text)
video_url = data['contentUrl']
print(video_url)

PS I'm sorry if the question is stupid, or there is a banal mistake, I've spent a lot of time on Google, but I still haven't found a solution

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
origami1024, 2020-07-13
@DemchukAgry

The OP's code is fully functional if one letter is changed

import requests
import json
from bs4 import BeautifulSoup

url = 'https://www.tiktok.com/@golden_men_6/video/6803237854542712070'

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}


r = requests.get(url, headers=headers)

soup = BeautifulSoup(r.content, 'html.parser')
script = soup.find('script', attrs={'id': 'videoObject'})

data = json.loads(script.next)
video_url = data['contentUrl']
print(video_url)

spoiler

script.next вместо script.text

D
dadasay, 2020-07-13
@dadasay

import requests
import json
from bs4 import BeautifulSoup
url = ' https://www.tiktok.com/@golden_men_6/video/6803237... '
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64 ; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
script = soup.find('script', attrs={'id': 'videoObject'})
data = json.loads(str(script.contents[0]))
video_url = data['contentUrl']
print(video_url)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question