爬虫——Xpath

3.爬虫的具体步骤

1.需要数据对应的真实URL地址
2.找到对应的请求方式
3.使用第三方包,构造请求(注意需要进行伪装,headers的伪装,最重要的是需要进行浏览器伪装——User-Agent)
4发送请求
5.收到响应之后,需要对响应体数据进行解析(普通的页面text,如果是多媒体文件是content)
6.将拿到的页面或者多媒体文件按照需求进行提取(正则,Xpath等)
7..将获取到的目标文件进行保存

4.Xpath学习

4.1 html和xml的区别

xml不是html的替代,他们是为了不同的目的而设计的
xml被设计为传输和存储数据,其焦点是数据的内容
html被设计用来显示数据,其焦点是数据的外观
html重点在显示信息,而xml重点在传输信息

4.2 xml实例

<bookstore>   
<book category="COOKING">
  <title lang="en">Everyday Italian</title> 
  <author>Giada De Laurentiis</author> 
  <year>2005</year> 
  <price>30.00</price> 
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title> 
  <author>J K. Rowling</author> 
  <year>2005</year> 
  <price>29.99</price> 
</book>
<book category="WEB">
  <title lang="en">Learning XML</title> 
  <author>Erik T. Ray</author> 
  <year>2003</year> 
  <price>39.95</price> 
</book>
</bookstore>

例子中的根元素是 <bookstore>。文档中的所有 <book> 元素都被包含在 <bookstore> 中。<book> 元素有 4 个子元素:<title>、< author>、<year>、<price>。

4.3 路径表达

表达式说明
/代表从根节点选取
//从当前文档的任何位置找起,只要符合节点就行
@选取的是属性
.当前节点
当前节点的父节点

4.4 xpath—etree的作用

str1 = '''
<div>this is a div
'''

doc = etree.HTML(str1)
str2 = etree.tostring(doc)
print(str2)

展示:
b'<html><body><div>this is a div\n</div></body></html>'

注意点:
1、tostring()转换的字符串是bytes类型
2、lxml可以补齐缺失的HTML标签,还可以自动添加html 和 body标签

4.5 获取标签内数据

ret = html_doc.xpath('//span/i/text()')
print(ret)

注意点:
1、text()获取标签扩起来的内容
2、xpath返回的数据类型是一个列表

提取,编辑和轻松评估XPath查询。 XPath的助手很容易提取,编辑,并在任何网页评估XPath查询。 重要提示:安装此扩展后,必须重新加载任何现有的选项卡或重新启动Chrome浏览器扩展工作。 说明: 1.打开一个新的标签,并导航到任何网页。 2.按Ctrl-Shift键-X(或OS X命令移-X),或单击工具栏上的XPath的助手按钮,以打开XPath助手控制台。 3.按住Shift键将鼠标悬停在页面上的元素。查询框将不断更新,以显示鼠标指针下方的元件XPath查询,结果框将显示当前查询的结果。 4.如果需要,请在控制台直接编辑XPath查询。结果框会立即反映更改。 5.重复步骤(2)关闭控制台。 如果控制台在你的方式获得,按住Shift键,然后将鼠标移动到它; 它会移动到页面的相对侧。 一个忠告:当渲染HTML表格,浏览器插入人工<TBODY>标记到DOM,这将在随后通过该扩展提取查询显示出来。 Extract, edit, and evaluate XPath queries with ease. XPath Helper makes it easy to extract, edit, and evaluate XPath queries on any webpage. IMPORTANT: After installing this extension, you must reload any existing tabs or restart Chrome for the extension to work. Instructions: 1. Open a new tab and navigate to any webpage. 2. Hit Ctrl-Shift-X (or Command-Shift-X on OS X), or click the XPath Helper button in the toolbar, to open the XPath Helper console. 3. Hold down Shift as you mouse over elements on the page. The query box will continuously update to show the XPath query for the element below the mouse pointer, and the results box will show the results for the current query. 4. If desired, edit the XPath query directly in the console. The results box will immediately reflect your changes. 5. Repeat step (2) to close the console. If the console gets in your way, hold down Shift and then move your mouse over it; it will move to the opposite side of the page. One word of caution: When rendering HTML tables, Chrome inserts artificial <tbody> tags into the DOM, which will consequently show up in queries extracted by this extension.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值