Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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)
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 questionAsk a Question
731 491 924 answers to any question