【发布时间】:2020-05-05 05:37:04
【问题描述】:
我有一个很好的 URL 结构,我想遍历 URL 并从 URL 下载所有图像。我正在尝试使用 BeautifulSoup 以及 requests 函数来完成工作。
这里是 URL - https://sixmorevodka.com/#&gid=0&pid={i},对于这个例子,我希望“i”从 1 到 100 进行迭代。
from bs4 import BeautifulSoup as soup
import requests, contextlib, re, os
@contextlib.contextmanager
def get_images(url:str):
d = soup(requests.get(url).text, 'html.parser')
yield [[i.find('img')['src'], re.findall('(?<=\.)\w+$', i.find('img')['alt'])[0]] for i in d.find_all('a') if re.findall('/image/\d+', i['href'])]
n = 100 #end value
for i in range(n):
with get_images(f'https://sixmorevodka.com/#&gid=0&pid={i}') as links:
print(links)
for c, [link, ext] in enumerate(links, 1):
with open(f'ART/image{i}{c}.{ext}', 'wb') as f:
f.write(requests.get(f'https://sixmorevodka.com{link}').content)
我想我要么在 Yield 行或在最后一个 write 行搞砸了。请有人帮帮我。我正在使用 Python 3.7
【问题讨论】:
标签: python css python-3.x beautifulsoup