使用bs4 python刮取图像

时间:2020-09-23 18:09:42

标签: python beautifulsoup

这是链接“ https://www.blocket.se/annons/stockholm/samsung_note10__i_nyskick_med_kvitto/92119857”

我想提取手机图像

"<div style="left:0%;background-image:url(https://i.blocketcdn.se/pictures/2835058804.jpg?type=original);width:100%;height:100%;background-size:contain;background-position:50% 50%;background-repeat:no-repeat;position:absolute"></div>"

当我使用find_all搜索时,我得到的是背景图片,而不是我想要的那个(手机),或者没有

任何有想法如何仅提取电话图像的人

1 个答案:

答案 0 :(得分:0)

您可以从<meta ...>标签提取图片网址:

import requests
from bs4 import BeautifulSoup


url = 'https://www.blocket.se/annons/stockholm/samsung_note10__i_nyskick_med_kvitto/92119857'
soup = BeautifulSoup(requests.get(url).content, 'lxml')

img_url = soup.select_one('[property="og:image"]')['content']
img_url_original = img_url.split('?')[0] + '?type=original'

print(img_url_original)

打印:

https://i.blocketcdn.se/pictures/2835058804.jpg?type=original