博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xpath解析数据
阅读量:7223 次
发布时间:2019-06-29

本文共 3367 字,大约阅读时间需要 11 分钟。

from lxml import etree

html = etree.HTML(doc)
print(html.xpath("/bookstore")) # 从根标签开始找所有匹配的
print(html.xpath("//bookstore")) # 全文中找所有匹配的

# 通配符 *
print(html.xpath("//book"))
print(html.xpath("//*"))

# 获取属性
print(html.xpath("//bookstore/@id"))
print(html.xpath("//bookstore/@*"))

# 嵌套
print(html.xpath("//bookstore/book/title/text()"))

 

# 加上谓语(条件) ==========================================================================================

# 指定要获取的索引

# print(html.xpath("//bookstore/book[1]/title/text()")) # 获取第一个
# print(html.xpath("//bookstore/book[last()-1]/title/text()")) # last() 最后一个 last()-1 倒数第二个
# print(html.xpath("//bookstore/book[position()>1]/title/text()")) # 索引大于1的

# print(html.xpath("//book[price > 30]"))
# # xpath 原生 既能查找属性 又能查找标签 而在selenium只能查找标签
#
#
# # 查找price的值大于30的book标签
# e = html.xpath("//book[price > 30]")[0]
# print(type(e))
# from lxml.etree import _Element
# print(e.text) # 访问文本 不包含子标签的文本
# print(e.attrib) # 访问属性

# 用属性来作限制
# 只要存在lang属性即可
print(html.xpath("//*[@lang]"))

# 找的是具备lang并且值为abc的标签

print(html.xpath("//*[@lang='abc']")[0].attrib)

# 只要 有属性即可

print(html.xpath("//*[@*]"))

# 多个匹配条件

print(html.xpath("//title|//price"))

 

# 轴匹配 (先拿到一个标签 在相对这个标签找其他标签) ===========================================

print(html.xpath("//bookstore/ancestor::*")) # 所有先辈

print(html.xpath("//bookstore/ancestor::body")) # 所有叫body的先辈
print(html.xpath("//bookstore/ancestor-or-self::*")) # 所有叫body的先辈

# 获取属性
print(html.xpath("//bookstore/attribute::id"))
print(html.xpath("//bookstore/@id"))

# 所有子级标签

print(html.xpath("//bookstore/child::*"))

# 所有后代标签

print(html.xpath("//bookstore/descendant::*"))

# 在这个标签后面的所有标签 与层级无关

print(html.xpath("//book[1]/following::*"))
# 它弟弟们
print(html.xpath("//book[1]/following-sibling::*"))
# 它哥哥们
print(html.xpath("//book[1]/preceding-sibling::*"))

# 获取父级
# print(html.xpath("//book[1]/parent::*"))

# 获取既有id属性 又有class属性的标签
print(html.xpath("//*[@id and @class]"))

from selenium import webdriver

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

# 创建配置对象

chrome_options = Options()
# chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
# chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 可以提升速度

driver = webdriver.Chrome(r"D:\jerry\spiderDay3\selenium模块\chromedriver.exe",options=chrome_options)

driver.get("https://www.baidu.com")

#

# img = driver.find_element_by_css_selector(".index-logo-src")
# img = driver.find_element_by_xpath("//img[@class='index-logo-src']")
# print(img.get_attribute("src"))

# 四套解析数据的方式
# 1.bs4 2.css_selector 3.xpath 4.re

 

由于有些网站使用懒加载来提升速度影响爬取数据,需要滑动屏幕到指定位置再获取数据

这就需要将屏幕滑到底部

# for i in range(1000):

# driver.find_element_by_tag_name("body").send_keys(Keys.DOWN)
# print(i)

# 平滑滚动到底部 加载所有数据到页面 速度太快没有加载完整

# driver.execute_script("""
# window.scrollTo({
# top: document.body.clientHeight,
# behavior: "smooth"
# });
# """)

# 慢慢滑动

# 执行js获取总高度
height = driver.execute_script("return document.body.clientHeight")
# print(height,type(height))
# 已经滑动的距离
dis = 0
while dis < height:
driver.execute_script("""
window.scrollTo({
top: %s,
behavior: "smooth"
});""" % dis)
dis += 200
time.sleep(0.2)
driver.implicitly_wait(5)

转载于:https://www.cnblogs.com/suncunxu/p/10698271.html

你可能感兴趣的文章
Linux+Redis实战教程_day03_1、Redis-LinkedList【重点】
查看>>
spring-cloud feign (web服务客户端)(四)
查看>>
软件架构视图—4+1视图模式
查看>>
迁移笔记:php缓存技术memcached
查看>>
约束,自定义异常,hashlib,logging
查看>>
响应式布局:Flexbox应用总结
查看>>
话说程序员的职业生涯
查看>>
.NET深入解析LINQ框架(一:LINQ优雅的前奏)[转载]
查看>>
[转]linux命令后台运行
查看>>
20060507: 视频教程:写一个简单“Hello, world”的Java程序
查看>>
Linux服务器管理: RPM包
查看>>
AC日记——狼抓兔子 bzoj 1001
查看>>
AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936
查看>>
体验Azure的 Automation “自动化” 服务预览版
查看>>
关于在各种int类型选择时的考虑
查看>>
零起步的Hadoop实践日记(hbase in action)
查看>>
关于meta 总结
查看>>
C#颜色选择器的调用操作
查看>>
百度云服务的使用
查看>>
bzoj千题计划298:bzoj3997: [TJOI2015]组合数学
查看>>