爬虫 xpath 获取方式
回顾 bs4
- 实例化bs对象,将页面源码数据加载到该对象中
- 定位标签:find('name',class_='xxx') findall() select()
- 将标签中的文本内容获取 string text get_text() a['href']
xpath
环境安装: pip install lxml
原理解析:
获取页面的源码数据
实例化etree对象,并将页面源码数据加载到该对象中
调用该对象xpath方法进行指定标签的定位
注意:xpath必须结合者xpath的表达式进行标签定位和内容捕获
/html/head/title
//head/title
//title
通过xpath进行获取数据
#项目需求:解析58二手房的相关数据
import requests
from lxml import etree url = '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).text tree = 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[3]//text()')
price = ''.join(price)
fp.write(title+":"+price+'\n')
fp.close()
print('over') #调用xpath 返回的是一个列表结构,使用索引
利用xpath处理中文乱码
# ctrl+shift+x
# - 解析图片数据:http://pic.netbian.com/4kmeinv/
import requests
from lxml import etree
import os
import urllib url = '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.encoding = 'utf-8'
if not os.path.exists('./imgs'):
os.mkdir('./imgs')
page_text = response.text tree = 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!!!') #通过encode('iso-8859-1').decode('gbk')编译
#或使用response.encoding = 'utf-8'
xpath在遇到加密base64时解决加密a标签
#【重点】下载煎蛋网中的图片数据:http://jandan.net/ooxx
#数据加密 (反爬机制)
import requests
from lxml import etree
import base64
import urllib 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'
}
url = 'http://jandan.net/ooxx'
page_text = requests.get(url=url,headers=headers).text tree = etree.HTML(page_text)
img_hash_list = tree.xpath('//span[@class="img-hash"]/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)
xpath获取两次a标签进行获取及分页判断
#爬取站长素材中的简历模板
import requests
import random
from lxml import etree
headers = {
'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.text tree = 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).text
tree = 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).content
fileName = name+'.rar'
with open(fileName,'wb') as fp:
fp.write(data)
print(fileName,'下载成功') //*[@id="down"]/div[2]/ul/li[6]/a
xpath 利用 | 实现并集获取数据
#解析所有的城市名称
import requests
from lxml import etree
headers = {
'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).text tree = 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)
proxies 代理设置
#设置请求的代理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'}).text with open('./ip.html','w',encoding='utf-8') as fp:
fp.write(page_text)
防卫机制:
robots
UA
数据加密
懒加载
代理ip
爬虫 xpath 获取方式的更多相关文章
- Scrapy:运行爬虫程序的方式
Windows 10家庭中文版,Python 3.6.4,Scrapy 1.5.0, 在创建了爬虫程序后,就可以运行爬虫程序了.Scrapy中介绍了几种运行爬虫程序的方式,列举如下: -命令行工具之s ...
- 放养的小爬虫--京东定向爬虫(AJAX获取价格数据)
放养的小爬虫--京东定向爬虫(AJAX获取价格数据) 笔者声明:只用于学习交流,不用于其他途径.源代码已上传github.githu地址:https://github.com/Erma-Wang/Sp ...
- Appium根据xpath获取控件
如文章< Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通 ...
- Appium依据xpath获取控件实例随笔
如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.当中一种就是依据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...
- 【转】Appium根据xpath获取控件实例随笔
原文地址:http://blog.csdn.net/zhubaitian/article/details/39754233 如文章<Appium基于安卓的各种FindElement的控件定位方法 ...
- Appium根据xpath获取控件实例随笔
如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...
- 使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接
使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接: 使用requests获取html后,分析html中的标签发现所需要的链接在& ...
- 爬虫, 获取登录者的外网IP
笔者学习了一下用爬虫, 获取登录者的外网IP. 首先导入Jsoup的jar包 public class RetrivePage { private static String url="ht ...
- [转]Android SHA1与Package获取方式
转自高德地图LBS Android SHA1与Package获取方式 获取应用包名 打开Android 应用工程的 AndroidManifest.xml配置文件,package 属性所对应的内容为应 ...
随机推荐
- TP5 查询mysql数据库时的find_in_set用法
$where['class_id'] = ['in', '$cid_all']; $where['id'] = ['in', $all_user_id];//或这样子 $where['title'] ...
- Linux-3.14.12内存管理笔记【建立内核页表(1)】
前面已经分析过了Intel的内存映射和linux的基本使用情况,已知head_32.S仅是建立临时页表,内核还是要建立内核页表,做到全面映射的.下面就基于RAM大于896MB,而小于4GB ,切CON ...
- SSH整合二
结构图 articles模块 实体类Articles.java package com.jt.articles.entity; public class Articles { private Inte ...
- leetcode 752. 打开转盘锁
地址 https://leetcode-cn.com/problems/open-the-lock/ 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', ...
- php date获取前一天的时间
结果: 结论: 第二种方式只使用了一个函数,所以更快一些,速度大约是第一种的两倍
- Fuzzy finder(fzf+vim) 使用入门指南
今天无意中尝试了fzf,才发现这个工具的威力无穷,毕竟是非常好的工具,第一次都把它的优点都释放出来也不现实,先熟悉一下吧,后面在实战中再不断地学习总结. 它是什么: Fuzzy finder 是一款使 ...
- 【计算机网络】WebSocket实现原理分析
1.介绍一下websocket和通信过程? 1.1 基本概念 [!NOTE] Websocket是应用层第七层上的一个应用层协议,它必须依赖 HTTP 协议进行一次握手 ,握手成功后,数据就直接从 T ...
- TCP协议 - 可靠性
在前篇文章中介绍了TCP协议的三大特性,其中可靠性是依赖一系列的机制,如:校验和,分组发送,超时重传,流量控制得到保证. 一.数据交互 TCP在交互数据时,采用多种机制保证可靠性,同时也保证TCP的性 ...
- 接口的 COM 组件调用 QueryInterface 因以下错误而失败: 库没有注册。
这个问题原因是因为安装了高版本的office然后卸载掉,又安装了低版本的office导致的. 博主是 office2016卸载后,安装了office2013. EXCEL报错信息为: 无法将类型为“M ...
- Linux问题记录——主机名变成了bogon
Linux问题记录——主机名变成了bogon 摘要:本文主要记录了主机名变成bogon的原因以及解决办法. 问题重现 主机名在一次登录后,变成了bogon,此后每次登录Linux系统时都是bogon. ...