Fetch image address using python request, beautifulsoup

48 Views Asked by At

I tried to fetch some pictures from a website using Python but it is not easy to me.

Here is what I did below:

import requests
from bs4 import BeautifulSoup
import re

url = "https://www.mayde.com/ponytail"
res = requests.get(url)
res.raise_for_status()
soup = BeautifulSoup(res.text, "lxml")
item_address1 = soup.find_all("div",attrs={"class":"BEmRci heightByImageRatio heightByImageRatio4"})
print(item_address1[0]["style"])

The result is:

background-image:url(https://static.wixstatic.com/media/42be90_61424ae0ff87475a8c536ba3afe1410d~mv2.jpg/v1/fill/w_64,h_100,al_c,q_80,usm_0.66_1.00_0.01/42be90_61424ae0ff87475a8c536ba3afe1410d~mv2.jpg);background-size:contain

I want:

https://static.wixstatic.com/media/42be90_61424ae0ff87475a8c536ba3afe1410d~mv2.jpg/v1/fill/w_64,h_100,al_c,q_80,usm_0.66_1.00_0.01/42be90_61424ae0ff87475a8c536ba3afe1410d~mv2.jpg
1

There are 1 best solutions below

0
On

Just basic string processing.

i = item_address1.find('(')
j = item_address1.find(')')
item_address1 = item_address1[i+1:j]