爬虫三
搜索文档树
1 、find_all :找所有 列表
2、find 找一个 Tag类的对象
无论按标签名,按属性,按文本内容 都是按正则形式查找
找到所有名字以b开头的所有标签
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')
爬图片
import requests
from bs4 import BeautifulSoup
import requests
from bs4 import BeautifulSoup
res = requests.get('https://pic.netbian.com/tupian/32518.html') res.encoding = 'gbk' # print(res.text) soup = BeautifulSoup(res.text, 'html.parser') ul = soup.find('ul', class_='clearfix') img_list = ul.find_all(name='img', src=True) for img in img_list: try: url = img.attrs.get('src') if not url.startswith('http'): url = 'https://pic.netbian.com' + url print(url) res1=requests.get(url) name=url.split('-')[-1] with open('./img/%s'%name,'wb') as f: for line in res1.iter_content(): f.write(line) except Exception as e: continue
-.ini
-.yaml
-.xml
2、find_all 其他参数
-limit=数字 找几条 ,如果写1 ,就是一条
-recursive
3 、搜索文档树和遍历文档树可以混用,找属性,找文本跟之前学的一样
id选择器
#id号
标签选择器
标签名
类选择器
.类名
记住的:
#id
.sister
head
div>a # div下直接子节点a
div a # div下子子孙孙节点a
一旦会了css选择器的用法---》以后所有的解析库都可以使用css选择器去找
import requests from bs4 import BeautifulSoup res = requests.get('https://www.cnblogs.com/liuqingzheng/p/16005896.html') # print(res.text) soup = BeautifulSoup(res.text, 'html.parser') # a=soup.find(name='a',title='下载哔哩哔哩视频') # print(a.attrs.get('href')) # p=soup.select('#cnblogs_post_body p:nth-child(2) a:nth-child(5)')[0].attrs.get('href') # p=soup.select('#cnblogs_post_body > p:nth-child(2) > a:nth-child(5)')[0].attrs.get('href') # 以后直接复制即可 p=soup.select('a[title="下载哔哩哔哩视频"]')[0].attrs.get('href') # 以后直接复制即可 复制selector print(p)
selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题
selenium 会做web方向的自动化测试
appnium 会做 app方向的自动化测试
selenium 可以操作浏览器,模拟人的 行为
如何使用:
1、下载浏览器驱动:
谷歌驱动:
https://googlechromelabs.github.io/chrome-for-testing/
https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/119.0.6045.105/win64/chromedriver-win64.zip
edge驱动:
Microsoft Edge WebDriver - Microsoft Edge Developer
跟浏览器型号和版本一一对应的
ie,火狐,谷歌:谷歌为例
谷歌浏览器有很多版本:跟版本一一对应
2、安装 selenium
3、写python代码,操作浏览器
例子(打开浏览器)
import time from selenium import webdriver # 跟人操作浏览器一样,打开了谷歌浏览器,拿到浏览器对象 bro=webdriver.Chrome() # 在地址栏中输入地址 bro.get('https://www.baidu.com') time.sleep(5) bro.close()
import time from selenium import webdriver from selenium.webdriver.common.by import By bro = webdriver.Chrome() bro.get('https://www.baidu.com') bro.implicitly_wait(10) # 设置等待---》从页面中找标签,如果找不到,就等待 # 最大化 bro.maximize_window() # print(bro.page_source) # 当前页面的html内容 # 找到登录按钮--》选择器---》css选择器 # a_login=bro.find_element(by=By.NAME,value='tj_login') # a_login=bro.find_element(by=By.ID,value='s-top-loginbtn') a_login = bro.find_element(by=By.LINK_TEXT, value='登录') # a 标签连接文字 time.sleep(2) # 点击 a_login.click() # 找到短信登录 点击 sms_login = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__changeSmsCodeItem') sms_login.click() time.sleep(1) user_login = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__changePwdCodeItem') user_login.click() time.sleep(1) username = bro.find_element(by=By.NAME, value='userName') # 往输入框中写文字 username.send_keys('lqz@qq.com') password = bro.find_element(by=By.ID, value='TANGRAM__PSP_11__password') # 往输入框中写文字 password.send_keys('lqz@qq.com') agree = bro.find_element(By.ID, 'TANGRAM__PSP_11__isAgree') agree.click() time.sleep(1) submit = bro.find_element(By.ID, 'TANGRAM__PSP_11__submit') submit.click() time.sleep(3) bro.close()
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度 chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 bro = webdriver.Chrome(options=chrome_options) bro.get('https://www.cnblogs.com/liuqingzheng/p/16005896.html') print(bro.page_source) time.sleep(3) bro.close()
print(div.location) # 在页面中位置: x y轴效果---》
print(div.tag_name) # 标签名
print(div.size) # 标签大小 x y
print(div.text) # 文本内容
找到页面中所有div
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能