爬虫第二弹 图片解析
reptile_second
- 知识点回顾
- robots.txt:
UA:
- 1.指定url
- 2.发起请求
- 3.获取页面数据
- 4.数据解析
- 5.持久化存储
- bs4:
- 实例化bs对象,将页面源代码数据加载到该对象中
- 定位标签:find('name',class_='someone') findall() select()
- 将标签中的文本内容获取,strint,text get_txtx() a['href']
- 环境安装: pip install lxml
- 分析原理:
- 获取页面源码数据
- 实例化一个etree的对象,并且将页面源码数据加载到哦该对象中
- 调用该对象的xpath方法进行制定标签的定位
- 注意: xpath函数必须结合着xpath表达式进行标签定位和内容捕获
项目需求: 解析58 二手房的相关数据
import requests
from lxml import etreeurl = 'https://bj.58.com/shahe/ershoufang/?utm_source=market&spm=u-2d2yxv86y3v43nkddh1.BDPCPZ_BT&PGTID=0d30000c-0047-e4e6-f587-683307ca570e&ClickID=1'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).texttree =etree.HTML(page_text)
li_list = tree.xpath('//ul[@class="house-list-wrap"]/li')
fp = open('58.csv','w',encoding='utf-8')
for li in li_list:title = li.xpath('./div[2]/h2/a/text()')[0]price = li.xpath('./div[2]//text()')price = ''.join(price)fp.write(title + ':' + price + '\n')
fp.close()
print('over')
解析图片数据: http://pic.netbian.com/4kmeinv/
# ctrl + shift + x
# - 解析图片数据: http://pic.netbian.com/4kmeinv/
import os
import urllib
import requests
from lxml import etreeurl = 'http://pic.netbian.com/4kmeinv/'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}response = requests.get(url=url,headers=headers)
# response.encording = 'utf-8'if not os.path.exists('./imgs'):os.mkdir('./imgs')
page_text = response.texttree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="slist"]/ul/li')
for li in li_list:img_name = li.xpath('./a/b/text()')[0]# 处理中文乱码img_name = img_name.encode('iso-8859-1').decode('gbk')img_url = 'http://pic.netbian.com' + li.xpath('./a/img/@src')[0]img_path = './imgs/' + img_name +'.jpg'urllib.request.urlretrieve(url=img_url,filename=img_path)print(img_path, '下载成功!')
print('over!!')
# [重点] 下载煎蛋网中的图片数据:http://jandan.net/ooxx
# [重点] 下载煎蛋网中的图片数据:http://jandan.net/ooxx
# 数据加密 (反扒机制)
import base64
import urllib
import requests
from lxml import etreeurl = 'http://jandan.net/ooxx'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
page_text = requests.get(url=url,headers=headers).texttree = etree.HTML(page_text)
img_hash_list = tree.xpath('//span[@class="img-has"]/text()')
for img_hash in img_hash_list:img_url = 'http:' + base64.b64decode(img_hash).decode()img_name = img_url.split('/')[-1]urllib.request.urlretrieve(url=img_url,filename=img_name)print('over!')
爬取站长素材中的简历模板
import random
import requests
from lxml import etreeheaders = {'Connection':'close', #当请求成功后,马上断开该次请求(及时释放请求池中的资源)'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}url = 'http://sc.chinaz.com/jianli/free_%d.html'
for page in range(1,4):if page ==1:new_url = 'http://sc.chinaz.com/jianli/free.html'else:new_url =format(url%page)response = requests.get(url=new_url,headers=headers)response.encoding = 'utf-8'page_text = response.texttree = etree.HTML(page_text)div_list = tree.xpath('//div[@id="container"]/div')for div in div_list:detail_url = div.xpath('./a/@href')[0]name = div.xpath('./a/img/@alt')[0]detail_page = requests.get(url=detail_url,headers=headers).texttree = etree.HTML(detail_page)download_list = tree.xpath('//div[@class="clearfix mt20 downlist"]/ul/li/a/@href')download_url = random.choice(download_list)data = requests.get(url=download_url,headers=headers).contentfileName = name + '.rar'with open(fileName,'wb') as fp:fp.write(data)print(fileName,'下载成功')print('GG')
解析所有城市名称
import requests
from lxml import etreeheaders = {'Connection':'close', #当请求成功后,马上断开该次请求(及时释放请求池中的资源)'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'
}
url = 'https://www.aqistudy.cn/historydata/'
page_text = requests.get(url=url,headers=headers).tetxtree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="bottom"]/ul/li | //div[@class="bottom"]/ul/div[2]/li')for li in li_list:city_name = li.xpath('./a/text()')[0]print(city_name)
代理ip
# 设置请求代理ip: www.goubanjia.com 诀代理 西祠代理
# 代理ip的类型必须和请求url 的协议保持一致
url = 'https://www.baidu.com/s?wd=ip'page_text = requests.get(url=url,headers=headers,proxies={'https':'61.7.170.240:8080'}).textwith open('./ip.html','w',encoding='utf-8') as fp:fp.write(page_text)
tips
- robots
- UA
- 数据加密
- 懒加载
- 代理ip
转载于:https://www.cnblogs.com/zzy7372/p/10453049.html
爬虫第二弹 图片解析相关推荐
- 爬虫第二弹:千图网电商淘宝模板图片下载
爬虫第二弹:千图网电商淘宝模板图片下载 一.功能分析: 1.下载千图网电商淘宝的所有模板图片要求是高清版本: 2.并按照主页面将图片归类文件夹. 二.思路分析: 1.利用scrapy构建scra ...
- python爬虫第二弹-多线程爬取网站歌曲
python爬虫第二弹-多线程爬取网站歌曲 一.简介 二.使用的环境 三.网页解析 1.获取网页的最大页数 2.获取每一页的url形式 3.获取每首歌曲的相关信息 4.获取下载的链接 四.代码实现 一 ...
- 02.Python网络爬虫第二弹(http和https协议)
02.Python网络爬虫第二弹<http和https协议> 一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩 ...
- 02.Python网络爬虫第二弹《http和https协议》
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- Python网络爬虫第二弹《http和https协议》
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- 爬虫第二弹之http协议和https协议
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- 02,Python网络爬虫第二弹《http和https协议》
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- 图片url解析正确,但爬虫无法下载图片
图片url解析正确,但爬虫无法下载图片 爬虫错误debug 解注释DOWNLOADER_MIDDLEWARES HTTP status code is not handled or not allow ...
- Android ANR 问题第二弹------Input超时实战问题解析上
在前面的Android ANR 问题第二弹一文中,我们简诉了Android Input超时的原因,我们了解到系统Input系统分发Input的事件时如果有5s超时会触发应用ANR.在实际开发测试中,我 ...
最新文章
- flash cs 3组件学习笔记
- Linux命令行文档查看cat、less、more、head、tail和图片查看
- (转)JAVA正则表达式语法大全
- Java原生序列化、Avro、RPC与Log4j
- Maven多模块项目搭建
- inode mac客户端_淘宝直播PC客户端适合哪些场景使用?
- python比java_对比java和python对比
- python计算moran_空间自相关 (Global Moran's I)
- win10录屏_不需要第三方软件,看看WIN10自带的几个强大的截图、录屏工具
- 善领dsa2020最新车机ce版_科技测丨需要在车机和手机中“二选一”的凯迪拉克
- 一文搞明白DNS与域名解析
- 我的世界服务器无限刷东西指令,我的世界无限刷物品命令方块指令 | 手游网游页游攻略大全...
- footnote latex
- C语言学习笔记(自用)(1):初识C语言
- Testin云层天咨众测学院开课了!
- mmap函数和munmap函数
- WEB前端:(3)HTML5②超链接及页面内跳转
- 每个公司、每个社区,都需要不止一个“灵魂人物”。
- cogs 998. [東方S2] 帕秋莉·诺蕾姬
- leetcode上奇怪的解答错误
热门文章
- php图片比例不失真,PHP中图片实现等比例不失真缩放
- 华为最新「天才少年」:26岁年薪百万,博士四年21篇论文
- CNN02:Pytorch实现VGG16的CIFAR10分类
- 程序开发常用第三方类库一览表(VendorLib)
- Route map应用策略路由(上)
- 使用LIRe来实现基于多特征描述符的图像检索系统
- dell pc restore 修复计算机,如何创建和使用Dell Recovery Restore USB驱动器
- 高考选日语可以学计算机吗,高考日语不能选的专业
- 详解CSS position 属性
- 利用oc门或od门实现线与_TTL电平,CMOS电平,OC门,OD门基础知识