我们可以从 JustWatch 网站 https://www.justwatch.com/in/movie/oppenheimer 的 IMDB 图像获取 IMDB 链接吗?
当我检查 IMDB 的图像元素时,没有 IMDB 链接。
但是,当我点击它时,它可以打开IMDB链接https://www.imdb.com/title/tt15398776/?ref_=justwatch。
有没有办法使用Python来抓取未显示在检查视图中的链接?
提前谢谢您。
这是我的代码,只能获得评级
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
url = "https://www.justwatch.com/in/movie/oppenheimer"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = BeautifulSoup(webpage, 'html.parser')
soup.select('div.jw-scoring-listing__rating span span')[1]
您可以尝试使用外部
regex
检查脚本元素的内容 imdbId
:
from urllib.request import Request, urlopen
import re
match = re.search(r"\"imdbId\":\s*\"([^\"]+)\"", str(webpage))
if match:
imdb_id_value = match.group(1)
print(imdb_id_value)
else:
print('no imdbId found')
或者将内容转换为 JSON 并将其视为字典:
...
json.loads(soup.select_one('script:-soup-contains("APOLLO_STATE")').text.strip('window.__APOLLO_STATE__='))
...