xpath解析数据

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:jerryspiderDay3selenium模块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