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)

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐