如何使用 python 从网页提要下载图像
How to download an image from a webpage feed with python
我对 python 有点陌生,我正在尝试编写一个脚本来从提要中获取第一张图片(这意味着这张图片每隔几个小时就会发生变化,所以我不能只需使用其 url) 并将其下载到指定文件即可。
这是我目前写的
import requests
res = requests.get('image_url')
with open('./folder/img.png', 'wb') as f:
f.write(res.content)
print('Done!')
我不想像上面那样直接放置图片 url,而是让它自动从网站获取图片。
尝试使用请求模块
https://requests.readthedocs.io/en/master/user/quickstart/#raw-response-content
您可以使用以下命令安装它:pip install requests
编辑
是哪个网站?
我最终使用请求模块获取网页,然后 BeautifulSoup4 模块对其进行解析,最后使用正则表达式匹配任何 post/image url
import bs4, requests, re
res = requests.get('https://www.reddit.com/')
page = bs4.BeautifulSoup(res.text, 'html.parser')
mo = [img for img in page.find_all('img', {'alt': 'Post image'})]
urlReg = re.compile(r'https://preview.redd.it/\w+.jpg?\S+')
links = urlReg.findall(str(mo))
我仍然有一个问题,有时正则表达式与页面中的任何 url 都不匹配,尽管我很确定那里有多个 url,我必须再次 运行 它直到它最终找到一个。
我对 python 有点陌生,我正在尝试编写一个脚本来从提要中获取第一张图片(这意味着这张图片每隔几个小时就会发生变化,所以我不能只需使用其 url) 并将其下载到指定文件即可。 这是我目前写的
import requests
res = requests.get('image_url')
with open('./folder/img.png', 'wb') as f:
f.write(res.content)
print('Done!')
我不想像上面那样直接放置图片 url,而是让它自动从网站获取图片。
尝试使用请求模块 https://requests.readthedocs.io/en/master/user/quickstart/#raw-response-content 您可以使用以下命令安装它:pip install requests
编辑
是哪个网站?
我最终使用请求模块获取网页,然后 BeautifulSoup4 模块对其进行解析,最后使用正则表达式匹配任何 post/image url
import bs4, requests, re
res = requests.get('https://www.reddit.com/')
page = bs4.BeautifulSoup(res.text, 'html.parser')
mo = [img for img in page.find_all('img', {'alt': 'Post image'})]
urlReg = re.compile(r'https://preview.redd.it/\w+.jpg?\S+')
links = urlReg.findall(str(mo))
我仍然有一个问题,有时正则表达式与页面中的任何 url 都不匹配,尽管我很确定那里有多个 url,我必须再次 运行 它直到它最终找到一个。