周末学习总结与作业
"""
我于黑暗中蛰伏,只为抓住黎明一瞬
Author:huihan
Time:2022/3/19 16:46
"""
import requests
from selenium.webdriver.common.keys import Keys
from lxml import etree
import time,os
from selenium.webdriver import Chrome,ChromeOptions
options=ChromeOptions()
options.add_experimental_option('excludeSwitches',['enable-automation'])
options.add_experimental_option('prefs',{"profile.managed_default_cotent_settings.images":2})
b=Chrome(options=options)
b.implicitly_wait(5)
b.get('https://lol.qq.com/data/info-heros.shtml#Navi')
time.sleep(2)
html= etree.HTML(b.page_source)
hero_name=html.xpath('//ul[@id="jSearchHeroDiv"]/li/a/@title')
# 所有英雄列表名字
o = 0
for x in hero_name:
#创建每个英雄文件夹
os.mkdir(f'F:\英雄联盟3/{x}')
#点击每个英雄
time.sleep(1)
hero=b.find_elements_by_xpath('//ul[@id="jSearchHeroDiv"]/li/a/img')[o]
print(hero)
hero.click()
time.sleep(1)
html1=etree.HTML(b.page_source)
key_animator0 = b.find_elements_by_xpath('//ul[@id="skinNAV"]/li/a/img')[1]
key_animator0.click()
time.sleep(1)
html2 = etree.HTML(b.page_source)
key_animator2 = html2.xpath('//ul[@id="skinBG"]/li/img/@src')
for s,y in enumerate(key_animator2):
result = requests.get(y)
key_animator1=open(f'F:\英雄联盟3/{x}/{x}皮肤{s}.png','wb')
key_animator1.write(result.content)
time.sleep(1)
return1=b.find_elements_by_xpath('//div[@class="website-path"]/a')[2]
return1.click()
o+=1
# 切换选项卡:浏览器对象.switch_to.window(窗口对象)
# 浏览器对象.window_handles-获取当前浏览器中所有的窗口对象,返回一个列表
from selenium.webdriver import Chrome,ChromeOptions
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
import time
options=ChromeOptions()
options.add_experimental_option('excludeSwitches',['enable-automation'])
options.add_experimental_option('prefs',{"profile.managed_default_cotent_settings.images":2})
b=Chrome(options=options)
b.implicitly_wait(5)
b.get('https://kns.cnki.net/kns8')
search=b.find_element_by_id('txt_search')
search.send_keys('Pthon')
search.send_keys(Keys.ENTER)
wait = WebDriverWait(b,5)
wait.until(expected_conditions.presence_of_element_located((By.CLASS_NAME,'result-table-list')))
search_result=b.find_elements_by_css_selector('.result-table-list>tbody>.odd>.name>a')
for x in search_result:
x.click()
time.sleep(2)
#切换窗口
b.switch_to.window(b.window_handles[1])
soup=BeautifulSoup(b.page_source,'lxml')
print(soup)
print('========')
print(b.page_source)
b.switch_to.window(b.window_handles[0])
"""
我于黑暗中蛰伏,只为抓住黎明一瞬
Author:huihan
Time:2022/3/18 9:14
"""
"""
pythom基于xpath数据解析需要使用的库是lxml
"""
# 1.lxml第三方库
"""
1)作用:lxml既可以解析xml内容也可以解析html内容
2)xpath相关术语
树 - 整个网页或者xml文件对应的结构
节点 -标签,元素
根节点 - 最外层的标签
绝对路径-从根节点开始写的路径
相对路径-用.表示当前节点
节点内容(内容)-双标签的标签内容
属性- 标签属性
"""
# 2.xml数据结构
"""
xml数据和json数据一样,是一种通用的数据格式.
"""
# 3. xpath语法-获取标签的语法
# 1)创建树并且获取根节点
# etree.XML(xml数据)-创建xml树结构并且返回根节点
# etree.HTML(html数据)-创建html树结构并且返回根节点
from lxml import etree
tree=etree.XML(open('超市.xml',encoding='utf-8').read())
# 2)通过路径获取标签:节点对象.xpath(路径)
"""
1.绝对路径: /绝对路径
2.相对路径:用.表示当前节点(xpath前面是哪个节点,当前节点就是谁);
用..表示当前节点的父节点
"""
# =============1.绝对路径=========================
# 绝对路径一定从根节点开始往下写,和xpath前面是哪个节点无关
g=tree.xpath('/supermarket/all_goods/goods/name')
print(g)
# 3)获取标签内容:获取标签的路径下加/text()可以返回列表数据,元素是路径下的标签
g=tree.xpath('/supermarket/all_goods/goods/name/text()')
print(g)
g=tree.xpath('/supermarket/all_goods/goods/count/text()')
print(g)
# =================2.相对路径=========
g1=tree.xpath('./all_goods/goods/name/text()')
print(g1)
all=tree.xpath('/supermarket/all_goods')[0]
result=all.xpath('./goods/name/text()')
print(result)
# ========3.任意路径=====
result=tree.xpath('//goods/name/text()')
print(result)
# 3)获取标签内容:获取标签的路径/text()
# 4)获取标签属性值:获取标签的路径/@属性名
res=tree.xpath('//staff/@position')
print(res)
staff=tree.xpath('//staff')[0]
# print(staff.xpath('./@position')[0],staff.xpath('./class')[0])
# 5)谓语-条件
# a.位置相关的条件
"""
标签名[N]-第N个指定标签
标签名[last()]-最后一个指定标签
标签名[last()-N]-倒数第(N+1)个标签
标签名[position()<N]-获取所有位置值(从一开始)在N前面的值
"""
result=tree.xpath('//all_goods/goods[1]/name/text()')[0]#只想要第一个name名称
print(result)
# 取倒数第二个
result=tree.xpath('//all_goods/goods[last()-1]/name/text()')[0]#只想要第一个name名称
print(result)
result=tree.xpath('//all_goods/goods[position()>1]/name/text()')[1:3]#只想要第一个name名称
print(result)
# b.和属性相关的条件
"""
标签名[@属性名]-获取拥有指定属性的指定标签
标签名[@属性名='值']-获取指定属性为指定属性值的指定标签
"""
result=tree.xpath('//goods[@class="c2"]/name/text()')
print(result)
# c.和标签内容相关的条件
"""
用子标签的标签内容对父标签进行筛选
# 标签对象[子标签名>数据]-通过标签中的子标签的内容来对标签进行筛选
# 标签对象[子标签名<数据]
# 标签对象[子标签名<=数据]
# 标签对象[子标签名>=数据]
# 标签对象[子标签名=数据]
"""
result=tree.xpath('//goods[pirce>3]/name/text()')#商品价格大于3的名称
print(result)
result=tree.xpath('//goods[name="巧克力"]/pirce/text()')#所有商品名字为巧克力的价格
print(result)
# 6)通配符-*
"""
用*代替任何标签或者任何属性
"""
result=tree.xpath('//*[@class="c1"]')#获取class为c2的所有标签
print(result)
版权声明:本文为qq_39547531原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。