前言
本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。
( 想要学习Python?Python学习交流群:1039649593,满足你的需求,资料都已经上传群文件流,可以自行下载!还有海量最新2020python学习资料。 )
本次目标
爬取娟娟壁纸网的图片
受难者地址
http://www.jj20.com/
环境
- Python3.6
- pycharm
爬虫代码
导入工具
import requests
import parsel
请求头
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
解析目标网站的数据
for page in range(1, 17):url = 'http://www.jj20.com/bz/ktmh/list_16_cc_14_{}.html'.format(page)response = requests.get(url=url, headers=headers)selector = parsel.Selector(response.text)lis = selector.css('body > div:nth-child(7) > ul li')for li in lis:page_url = 'http://www.jj20.com/' + li.css('a:nth-child(1)::attr(href)').get()title = li.css('a:nth-child(1) img::attr(alt)').get()get_img(page_url, title)
保存数据
def download(img_url, img_title):path = '保存地址' + title + '.jpg'response = requests.get(url=img_url, headers=headers)with open(path, mode='wb') as f:f.write(response.content)print(img_url, img_title)
运行结果