from selenium.webdriver import Chrome
# 1. 创建浏览器打开需要自动登录的网页
b = Chrome()
b.get('https://www.taobao.com')# 2.获取本地保存的cookiewithopen('files/taobao.txt', encoding='utf-8')as f:
result =eval(f.read())# 3.添加cookiefor x in result:
b.add_cookie(x)# 4.重新打开网页
b.get('https://www.taobao.com')input('end:')
import json
# xpath用来解析网页数据或者xml数据的一种解析方法,它是通过路径来获取标签(元素)。"""
Python数据:{'name': 'xiaoming', 'age': 18, 'is_ad': True, 'car_no': None}
Json数据:{"name": "xiaoming", "age": 18, "is_ad": true, "car_no": null}
xml数据:
<allStudent>
<student class="优秀学员">
<name>xiaoming</name>
<age>18</age>
<is_ad>是</is_ad>
<car_no></car_no>
</student>
<student class="优秀学员">
<name>xiaoming</name>
<age>18</age>
<is_ad>是</is_ad>
<car_no></car_no>
</student>
</allStudent>
"""# 1. 常见的几个概念"""
1)树:整个网页结构和xml结构就是一个树结构
2)元素(节点):html树结构的每个标签
3)根节点:树结构中的第一个节点
4)内容:标签内容
5)属性:标签属性
"""# 2. Xpath语法"""
1. 获取标签
1)绝对路径: 以'/'开头,然后从根节点开始层层往下写路径
2)相对路径: 写路径的时候用'.'或者'..'开头,其中'.'表示当前节点;'..'表示当前节点的父节点。
注意:如果路径以'./'开头,'./'可以省略
3)全路径: 以'//'开头的路径
2.获取标签内容:在获取标签的路径的最后加'/text()'
3.获取标签属性:在获取标签的路径的最后加'/@属性名'
"""# ==============================应用===================================from lxml import etree
# 1.创建树结构,获取根节点
html =open('data.html', encoding='utf-8').read()
root = etree.HTML(html)# 2.通过路径获取标签# 节点对象.xpath(路径) - 根据获取所有的标签,返回值是列表,列表中的元素是节点对象# 1)绝对路径
result = root.xpath('/html/body/div/a')print(result)# 获取标签内容
result = root.xpath('/html/body/div/a/text()')print(result)# 获取标签属性
result = root.xpath('/html/body/div/a/@href')print(result)# 1)绝对路径的写法跟xpath前面用谁去点的无关
div = root.xpath('/html/body/div')[0]
result = div.xpath('/html/body/div/a/text()')print(result)print('--------------------------------华丽的分割线-------------------------------------')# 2)相对路径
result = root.xpath('./body/div/a/text()')print(result)
result = div.xpath('./a/text()')print(result)
result = div.xpath('a/text()')print(result)# 3)全路径
result = root.xpath('//a/text()')print(result)
result = div.xpath('//a/text()')print(result)
result = root.xpath('//div/a/text()')print(result)print('--------------------------------华丽的分割线-------------------------------------')# 3.加谓语(加条件) - 路径中的节点[]# 1)位置相关谓语"""
[N] - 第N个指定标签(N从1开始)
[last()] - 最后一个指定标签
[last()-N]
[position()>N]、[position()>=N]、[position()<N]、[position()<=N]
"""
result = root.xpath('//span/p[2]/text()')print(result)
result = root.xpath('//span/p[last()]/text()')print(result)
result = root.xpath('//span/p[position()<=2]/text()')print(result)
result = root.xpath('//span/p[position()>2]/text()')print(result)
result = root.xpath('//span/p[last()-1]/text()')print(result)print('--------------------------------华丽的分割线-------------------------------------')# 2)属性相关谓语"""
[@属性名=属性值]
"""
result = root.xpath('//span/p[@id="p1"]/text()')print(result)
result = root.xpath('//span/p[@class="c1"]/text()')print(result)
result = root.xpath('//span/p[@data="5"]/text()')print(result)# 4.通配符# 在xpath中可以通过*来表示任意标签或者任意属性
result = root.xpath('//span/*/text()')print(result)
result = root.xpath('//span/p[@class="c1"]/text()')print(result)
result = root.xpath('//span/*[@class="c1"]/text()')print(result)
result = root.xpath('//span/span/@*')print(result)
result = root.xpath('//*[@class="c1"]/text()')print(result)