python爬虫示例-批量下载图片
python爬虫示例#!/usr/bin/python# -*- coding: UTF-8 -*-import os, sys, timeimport urllib.request, requests, bs4'''下载文件'''def downfiles(imglist):x = 0# 遍历for imgurl in imglist:...
·
python爬虫示例
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys, time
import urllib.request, requests, bs4
'''
下载文件
'''
def downfiles(imglist):
x = 0
# 遍历
for imgurl in imglist:
# 获取获得的从imglist中遍历得到的imgurl
imgres = requests.get(imgurl)
with open("D:\\360Downloads\\{}.jpg".format(x), "wb") as f:
f.write(imgres.content)
x += 1
print("第", x ,"张")
print("下载完毕")
# 获取网页源代码
url = 'https://www.zcool.com.cn/'
res = requests.get(url)
downloadedList = [] # 下载网址列表
res.raise_for_status()
html = bs4.BeautifulSoup(res.text, 'html5lib')
data = html.select('.card-img-hover > img') # 返回数组
for path in data:
target = path.get('src') # 返回src属性
target = target.split('@')[0] # 图片路径处理
downloadedList.append(target) # 加入全局数组
print(target)
print(type(downloadedList))
# 把网站的内容写下来
downfiles(downloadedList)
更多推荐
已为社区贡献4条内容
所有评论(0)