Web scraping 使用Soup刮取图像

Web scraping 使用Soup刮取图像,web-scraping,beautifulsoup,lazy-loading,Web Scraping,Beautifulsoup,Lazy Loading,我正在尝试从以下网站中获取图像:。目前的代码是: url = 'https://www.remax.ca/on/richmond-hill-real-estate/-2407--9201-yonge-st-wp_id268950754-lst' soup = BeautifulSoup(urlopen(url), 'html.parser') imgs = soup.findAll('div', attrs = {'class': 'images is-flex flex-one has-fl

我正在尝试从以下网站中获取图像:。目前的代码是:

url = 'https://www.remax.ca/on/richmond-hill-real-estate/-2407--9201-yonge-st-wp_id268950754-lst'
soup = BeautifulSoup(urlopen(url), 'html.parser')
imgs = soup.findAll('div',  attrs = {'class': 'images is-flex flex-one has-flex-align-center has-flex-content-center'})
当我查看
imgs
内部时,我找不到
图像活动ng star inserted ng lazyloaded
srcset
。因此,我无法下载图像


有人能建议如何解决这个问题吗

您可以使用xpath查找图像,并使用请求获取图像,然后将其写入文件,如下所示

导入请求
从lxml导入html
#向网站发送请求
r=请求。获取(“网站”)
#转换为html对象
tree=html.fromstring(r.content)
#从xpath查找图像URL
image\u url=tree.xpath(“xpaths/@href”)
#将每个图像写入计算机
对于图像中的i\u URL:
打开(“文件名”、“wb”)作为f:
f、 写作(一)

图像是延迟加载的,我认为问题在于此。所以我抓取了加载和管理这些图片的脚本

script = soup.find('script', {'type': 'application/ld+json'})
script_json = json.loads(script.contents[0])
imgs = script_json['@graph'][1]['photo']['url']

现在,
imgs
包含了您为该住宅提供的链接中的所有11张图片的列表。

这不起作用。我无法打开
local\u filename.jpg