飞道的博客

14行代码完成任意选择图片爬取

342人阅读  评论(0)

14行代码完成任意选择图片爬取

方法一

import requests
import re
# 获取源网页
url = 'http://www.duoziwang.com/head/wenzi/644597.html'
r = requests.get(url)
url_jpg = re.findall('src="(http.*?.jpg)"', r.text)
# 获取图片地址
for i in url_jpg:
    response = requests.get(i)
    filename = r"C:\Users\dell\Desktop\png\{}.png".format(i[-10:])
    # 下载保存图片
    with open(filename, "wb") as f:
        f.write(response.content)
        print('图片{}下载成功,已保存到桌面png文件夹中!'.format(i[-10:]))

```

方法二

import requests
import re
# 获取源网页
url = 'https://image.baidu.com/search/index?ct=201326592&cl=2&st=-1&lm=-1&nc=1&ie=utf-8&tn=baiduimage&ipn=r&rps=1&pv=&fm=rs4&word=%E6%88%91%E5%A7%93%E6%9B%B9&oriquery=%E6%88%91%E5%A7%93%E6%9B%B9%E7%9A%84%E9%9C%B8%E6%B0%94%E6%96%87%E5%AD%97%E5%9B%BE%E7%89%87&ofr=%E6%88%91%E5%A7%93%E6%9B%B9%E7%9A%84%E9%9C%B8%E6%B0%94%E6%96%87%E5%AD%97%E5%9B%BE%E7%89%87&hs=2&sensitive=0'
r = requests.get(url)
url_jpg = re.findall('"(https://.*?.jpg)"', r.text)
# 获取图片地址
j = 0
for i in url_jpg:
    j += 1
    response = requests.get(i)
    filename = r"C:\Users\dell\Desktop\png\{}.png".format(i[-24: -15])
    # 下载保存图片
    with open(filename, "wb") as f:
        f.write(response.content)
        print('图片{}下载成功,已保存到桌面png文件夹中!'.format(j))
print('******第{}张图片下载完成!******'.format(j))


转载:https://blog.csdn.net/cygqtt/article/details/106492518
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场