发布于2022-01-23 20:13 阅读(194) 评论(0) 点赞(0) 收藏(0)
import requests # 请求模块 第三方模块 pip install requests import parsel # 数据解析模块 第三方模块 pip install parsel import time # 时间模块 内置模块 time_1 = time.time() # 要什么用模块 首先要知道模块有什么用 for page in range(2, 12): print(f'====================正在爬取第{page}页的数据内容====================') url = f'http://www.netbian.com/1920x1080/index_{page}.htm' # 请求头: 把python代码伪装成浏览器对服务器发送请求 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36' } response = requests.get(url=url, headers=headers) # 出现乱码怎么办? 需要转码 # html_data = response.content.decode('gbk') response.encoding = response.apparent_encoding # 自动转码 # 获取源代码/获取网页文本数据 response.text # print(response.text) # 解析数据 selector = parsel.Selector(response.text) # CSS选择器 就是根据网页标签内容提取数据 # 第一次提取 提取所有的li标签内容 lis = selector.css('.list li') for li in lis: # http://www.netbian.com/desk/23397.htm title = li.css('b::text').get() if title: href = 'http://www.netbian.com' + li.css('a::attr(href)').get() response_1 = requests.get(url=href, headers=headers) selector_1 = parsel.Selector(response_1.text) img_url = selector_1.css('.pic img::attr(src)').get() img_content = requests.get(url=img_url, headers=headers).content with open('img\\' + title + '.jpg', mode='wb') as f: f.write(img_content) print('正在保存: ', title) time_2 = time.time() use_time = int(time_2) - int(time_1) print(f'总计耗时{use_time}秒')
我还给大家准备了这些资料,直接在这里免费领。
# 一群:872937351 (群满了的话加二群) # 二群:924040232 # python学习路线汇总 # 精品Python学习书籍100本 # Python入门视频合集 # Python实战案例 # Python面试题 # Python相关软件工具/pycharm永久激活
作者:38373
链接:https://www.pythonheidong.com/blog/article/1257604/bb35b60d00affcf4df00/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!