Trying to scrape posters from letterboxd-python

Viewed 29
response=page.text
soup=bs(response, 'html.parser')
script = soup.find_all("script", {"type": "application/ld+json"}, string="image")

The thing is i want to extract the image link from the script output.I am not familiar with CData. :(

1 Answers

This is one way to get the image url from that page:

import requests
from bs4 import BeautifulSoup as bs
import json

url = 'https://letterboxd.com/film/jojo-rabbit/'

r = requests.get(url)
soup = bs(r.text)

script_w_data = soup.select_one('script[type="application/ld+json"]')
json_obj = json.loads(script_w_data.text.split(' */')[1].split('/* ]]>')[0])
print(json_obj['image'])

Result:

https://a.ltrbxd.com/resized/film-poster/4/4/4/6/0/0/444600-jojo-rabbit-0-230-0-345-crop.jpg?v=a5ad083635

Requests docs: https://requests.readthedocs.io/en/latest/

For BeautifulSoup docs, go to https://beautiful-soup-4.readthedocs.io/en/latest/index.html

Related