bs4介绍,遍历文档树、搜索文档树、css选择器、selenium基本使用、selenium其他用法

发布时间 2023-04-17 16:14:11作者: Super小赵

昨日回顾

# 1 request 高级用法
    -解析json:发http的请求,返回的数据,可能是xml格式,json格式
        request.get().json()
        
    -ssl认证
       -http和https的区别
          https=http+ssl/tsl
       -http版本区别
          0.9:底层基于tcp,每次http请求,都是建立一个tcp连接,三次握手,请求结束需要四次挥手
          1.1:请求头中有个参数Keep-alive,可以保证多个http请求公用一个TCP连接
          2.x:多路复用,多个请求使用通一个数据包
        
    -代理:
       -发送请求,如果使用自己的ip,可能会被封(加黑名单),需要使用代理ip,
         -如果使用了代理ip,还能不能访问本地的django项目--->不能
            
         -res = requests.post('https://www.cnblogs.com',proxies={'http':'27.79.236.66:4001'})
         -高匿,透明
         -http请求头:x-forword-for,user-agent,cookie,referer,content-Type
         -http:
              -请求协议:
                  请求首行:请求头地址,请求方式,http的版本
                  请求头:key-value
                  请求体
              -相应协议:
                  响应首行:相应状态码,相应字符串描述
                  响应头:key-value,响应状态码,cookie
                  响应体
                
    -代理池:搭建免费代理池
        -开源的---》原理
              爬取免费代理---》验证---》存到redis中
              起了一个flask服务,监听5000,访问地址,就可以随机获取代理
     
    -自己的django 测试使用代理
    
    -超时
    
    -异常
    
    -上传文件
    
# 2 内网穿透
    -花生壳
    
# 3 爬取视频网站
    -请求头中的数据
    -请求回来的数据,不一定能直接用
    
# 4 爬取新闻
    -bs4:find_all,find

今日内容

  • bs4介绍,遍历文档树

  • bs4搜索文档树

  • css选择器

  • selenium基本使用

  • 无界面浏览器

  • selenium其它用法

1 bs4介绍,遍历文档树

# beautifulsoup4 
  从HTML或XML文件中提取数据的python库
  用它来解析爬取回来的xml

# 安装
  pip install beautifulsoup4
  
  pip install lxml  (解析库)
    
# soup=BeautifulSoup('要解析的内容str类型','html.parser/lxml')

1.1 bs4的遍历文档树

from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title">
superxz
<b>The Dormouse's story</b>
</p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1" name='superxz'>Elsie</a>
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup = BeautifulSoup(html_doc, 'lxml')

# 1 美化,不是标准xml,完成美化
print(soup.prettify())

# 2 遍历文档树---》通过 . 来遍历
print(soup.html.body.p)  # 一层一层找
print(soup.p)  # 跨层 只找第一个

# 3 获取标签名称
print(soup.a.name)

# 4 获取标签属性---》属性字典
print(soup.a.attrs['href'])
print(soup.a.attrs.get('class'))  # class 会有多个['class']
print(soup.a.attrs.get('name'))

# 5 获取标签的内容
# text   获取该标签内部子子孙孙所有的标签的文本内容
print(soup.p.text)

# string p下的文本只有一个时,取到,否则为None
print(soup.p.string)

# strings
print(list(soup.p.strings))  # generator 生成器

# 6 嵌套选择
print(soup.html.body)

# -----------了解
# 7 子节点,子孙节点
print(soup.body.contents)  # p下所有子节点,只取一层
print(list(soup.p.children))  # list_iterator得到一个迭代器,包含p下所有子节点  只取一层
print(list(soup.body.descendants))  # generator  子子孙孙

# 8 父节点、祖先节点
print(soup.a.parent)  # 获取a标签的父节点  直接父亲
print(list(soup.a.parents))  # 找到a标签所有的祖先节点,父亲的父亲,父亲的父亲的父亲...

# 9 兄弟节点
print(soup.a.next_sibling)  # 下一个兄弟
print(soup.a.previous_sibling)  # 上一个兄弟

print(list(soup.a.next_siblings))  # 下面的兄弟们=>生成器对象
print(list(soup.a.previous_siblings))  # 上面的兄弟们=>生成器对象

2 bs4搜索文档树

# find   find_all

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p id="my p" class="title"><b id="bbb" class="boldest">The Dormouse's story</b>
</p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html_doc, 'lxml')

# 五种过滤器: 字符串、正则表达式、列表、True、方法

# 1 字符串--->查询的条件是字符串
# res = soup.find_all(name='p')
res = soup.find_all('p')
print(res)
# 类名叫sister的所有标签
res = soup.find_all(class_='sister')
print(res)

# id 叫link1的标签
res = soup.find_all(id='link1')
print(res)

# 文本内容叫Elsie的父标签
res = soup.find(text='Elsie').parent
print(res)

# 另一种方式
res = soup.find_all(attrs={'class': 'sister'})
# res = soup.find_all(attrs={'id': 'link1'})
print(res)

# 2 正则表达式
import re
res = soup.find_all(id=re.compile('^l'))
# res = soup.find_all(class_=re.compile('^s'))
print(res)

# 3 列表
res = soup.find_all(id=['link1', 'link2'])
print(res)
print(soup.find_all(name=['a', 'b']))
print(soup.find_all(['a','b']))


# 4 True
res = soup.find_all(id=True)  # 所有有id的标签
# res = soup.find_all(href=True)
# res = soup.find_all(class_=True)
print(res)


# 5 方法
def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')

print(soup.find_all(name=has_class_but_no_id))

2.1 find的其他参数

name
class_
id
text
attrs
------------------
limit:限制调试,find_all用的  find本质是find_all limit=1
 
recursive:查找的时候,是只找第一层还是子子孙孙都找,默认是True,子子孙孙都找
# limit 参数
res = soup.find_all(href=True, limit=2)  # 只拿两条
print(res)

# recursive  查找的时候,是只找第一层还是子子孙孙都找
res = soup.find_all(name='b', recursive=False)
# res = soup.find_all(name='b')
print(res)

# 建议遍历和搜索一起用
res = soup.html.body.p.find_all(name='b', recursive=False)
print(res)

3 css选择器

# 之前学过css选择器,可以很复杂
  .类名
  # id
   p
    
# 咱们只学了bs4,以后可能见到别的解释器(lxml)--->他们都会支持css选择器,也会支持xpath
# bs4 支持css选择器
# html_doc = """
# <html><head><title>The Dormouse's story</title></head>
# <body>
# <p class="title">
#     <b>The Dormouse's story</b>
#     Once upon a time there were three little sisters; and their names were
#     <a href="http://example.com/elsie" class="sister" id="link1">
#         <span>Elsie</span>
#     </a>
#     <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
#     <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
#     <div class='panel-1'>
#         <ul class='list' id='list-1'>
#             <li class='element'>Foo</li>
#             <li class='element'>Bar</li>
#             <li class='element'>Jay</li>
#         </ul>
#         <ul class='list list-small' id='list-2'>
#             <li class='element'><h1 class='yyyy'>Foo</h1></li>
#             <li class='element xxx'>Bar</li>
#             <li class='element'>Jay</li>
#         </ul>
#     </div>
#     and they lived at the bottom of a well.
# </p>
# <p class="story">...</p>
# """
from bs4 import BeautifulSoup
# soup=BeautifulSoup(html_doc,'lxml')

# select内写css选择器
# print(soup.select('.sister'))

# print(soup.select('#link1'))
# print(soup.select('#link1 span'))


# 终极大招---》如果不会写css选择器,可以复制

import requests

res = requests.get('https://www.w3school.com.cn/css/css_selector_attribute.asp')
soup = BeautifulSoup(res.text, 'lxml')
# print(soup.select('#intro > p:nth-child(1) > strong'))
print(soup.select('#intro > p:nth-child(1) > strong')[0].text)

4 selenium介绍与基本使用

# selenium
selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题

selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等,来拿到网页渲染之后的结果,可支持多种浏览器

4.1 基本使用

# 使用步骤:
  1 下载selenium
  2 操作浏览器:分不同浏览器,需要下载不同浏览器的驱动
    用谷歌--->谷歌浏览器驱动:
        	-用谷歌---》谷歌浏览器驱动:
             https://registry.npmmirror.com/binary.html?path=chromedriver/
            -跟谷歌浏览器版本要对应  111.0.5563.65:
            
  3 下载完的驱动,放在项目路径下
4 写代码 控制谷歌浏览器

from selenium import webdriver
import time

bro = webdriver.Chrome(executable_path='chromedriver.exe')  # 打开一个谷歌浏览器

bro.get('https://www.douyin.com/')  # 在地址栏中输入地址
print(bro.page_source)  # 当前页面的内容 (html)
with open('1.html', 'w', encoding='utf-8')as f:
    f.write(bro.page_source)
time.sleep(5)
bro.close()  # 关闭浏览器

5 无界面浏览器

from selenium import webdriver
import time

from selenium.webdriver.chrome.options import Options

# 隐藏浏览器的图形化界面,但是数据还拿到
chrome_options = Options()
chrome_options.add_argument('window-size=1920x3000')  # 指定浏览器分辨率
chrome_options.add_argument('--hide-scrollbars')  # 隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false')  # 不加载图片, 提升速度
chrome_options.add_argument('--headless')  # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
# chrome_options.binary_location = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" #手动指定使用的浏览器位置


bro = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)  # 打开一个谷歌浏览器
# 隐藏浏览器的图形化界面,但是数据还拿到
bro.get('https://www.cnblogs.com/')  # 在地址栏中输入地址
print(bro.page_source)  # 当前页面的内容  (html格式)
time.sleep(5)
bro.close()  # 关闭浏览器

image-20230317214151823

5.1 模拟登录百度

from selenium import webdriver
import time
from selenium.webdriver.common.by import By

bro = webdriver.Chrome(executable_path='chromedriver.exe')  # 打开一个谷歌浏览器
bro.get('https://www.baidu.com')
# 加入等待:找标签,如果找不到,就等待 x秒,如果还找不到就报错
bro.implicitly_wait(10)  # 1 等待
# 从页面中找到登录 a标签,点击它
# By.LINK_TEXT  按a标签文本内容找
btn = bro.find_element(by=By.LINK_TEXT, value='登录')
# 点击它
btn.click()

# 找到按账号登录的点击按钮,有id,优先用id,因为唯一  TANGRAM__PSP_11__changePwdCodeItem
btn_2 = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__changeSmsCodeItem')
btn_2.click()
time.sleep(1)

btn_2 = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__changePwdCodeItem')
btn_2.click()
time.sleep(1)

name = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__userName')
password = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__password')
name.send_keys('123456789@qq.com')
password.send_keys('1234')
time.sleep(1)

submit = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__submit')
submit.click()
time.sleep(2)
bro.close()  # 关闭浏览器

6 selenium其它用法

6.1 查找标签

# 两个方法
bro.find_element   找一个
bro.find_elements  找所有

# 可以按id,标签名,name属性名,类名,a标签的文字,a标签的文字模糊匹配,css选择器,xpath【后面聊】
input_1=bro.find_element(by=By.ID,value='kw')  # 按id找

input_1 = bro.find_element(by=By.NAME, value='wd')  # name属性名

input_1=bro.find_element(by=By.TAG_NAME,value='input') # 可以按标签名字找

input_1=bro.find_element(by=By.CLASS_NAME,value='s_ipt') # 可以按类名

input_1=bro.find_element(by=By.LINK_TEXT,value='登录') # 可以按a标签内容找

input_1=bro.find_element(by=By.PARTIAL_LINK_TEXT,value='录') # 可以按a标签内容找

input_1 = bro.find_element(by=By.CSS_SELECTOR, value='#su')  # 可以按css选择器

6.2 获取位置属性大小,文本

print(tag.get_attribute('src'))  # 用的最多 拿标签的属性

tag.text  # 文本内容
#获取标签ID,位置,名称,大小(了解)
print(tag.id)
print(tag.location)
print(tag.tag_name)
print(tag.size)

6.3 等待元素被加载

# 代码执行很快,有的标签没来的及加载,直接查找就会报错,设置等待
# 隐士等待:所有标签,只要去找,找不到就遵循 等10s的规则
	bro.implicitly_wait(10)
# 显示等待:需要给每个标签绑定一个等待,麻烦

6.4 元素操作

# 点击
tag.click()

# 输入内容
tag.send_keys()

# 清空内容
tag.clear()

# 浏览器对象 最大化
bor.maximize_window()

# 浏览器对象 截全屏
bro.save_screenshot()

6.5 执行js代码

bro.execute_script('alert("美女")')  # 引号内部的相当于 用script标签包裹了

# 可以干的事
  -获取当前访问的地址 window.location
  -打开新的标签
  -滑动屏幕--->
bro.execute_script('scrollTo(0,document.documentElement.scrollHeight)')
  -获取cookie,获取定义的全局变量

6.6 切换选项卡

import time
from selenium import webdriver

browser=webdriver.Chrome(executable_path='chromedriver.exe')
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')

print(browser.window_handles) #获取所有的选项卡


browser.switch_to.window(browser.window_handles[1])
browser.get('https://www.taobao.com')
time.sleep(2)
browser.switch_to.window(browser.window_handles[0])
browser.get('https://www.sina.com.cn')
browser.close()

6.7 浏览器前进后退

import time
from selenium import webdriver

browser=webdriver.Chrome(executable_path='chromedriver.exe')
browser.get('https://www.baidu.com')
browser.get('https://www.taobao.com')
browser.get('http://www.sina.com.cn/')

browser.back()
time.sleep(2)
browser.forward()
browser.close()

5.7 异常处理

import time
from selenium import webdriver

browser=webdriver.Chrome(executable_path='chromedriver.exe')
try:
except Exception as e:
    print(e)

finally:
    browser.close()