爬虫——ajax和selenuim总结

为什么要写这个博客呢,这个代码前面其实都有,就是结束了。明天搞个qq登录,这个就结束了。

当然也会更新小说爬取,和百度翻译,百度小姐姐的爬取,的对比爬取。总结嘛!!!加油!!!

============================ajax====================================

,有时爬不到东西,可能是经过Ajax加载的数据,不是原始的HTML文档。

这样我们就要来模拟Ajax请求。

上实例:比如说我前几篇的,异步社区的爬取。

复制代码
    response = requests.get(url,headers=hearder,params=params).text

用的不光有url,headers,还有params,params中是对页数等的请求。

得到的是字典样子的数据,但是是字符串。

这就要用到json.loads(),来把字符串类型,转化为python的字典类型了

-----------------得到字典就是取值了。

同一个网站不用,params,就不会请求成功。



selenuim---用浏览器实现自动化(很强大的反爬工具)。

有些网站可能会有JavaScript动态加载数据,这种情况下,简单的获取初始HTML可能无法获取

这时就是用selenuim来模拟浏览器。


来复习一便selenuim自动化吧!!!-下一篇就是登录自动qq(目标)

第一篇代码:

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Edge()

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://www.baidu.com')
#寻找(异常的捕获)
try:
    element = wd.find_element(By.ID,'kw')
    element.send_keys('通讯')

    caozuo = wd.find_element(By.ID,'su')
    caozuo.click()#点击
    
    wd.quit()#退出
    input('等待回车键结束程序')

except NoSuchElementException:
    print('不存在')

1.导库-最后一个是异常

复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

2.这里try是如果抛出异常,就咋咋咋!

  1. wd.find-element(),caozuo.click()-------------------------很重要

---------------------二-------------------------------

第二篇代码:

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

url = 'https://cdn2.byhy.net/files/selenium/sample1.html'
# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Edge()

# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get(url)
#根据ID查找
id_element = wd.find_element(By.ID,'searchtext')
id_element.send_keys('haha')
input("jix1")
#根据class的名字查找++
elements = wd.find_elements(By.CLASS_NAME,'plant')
for i in elements:
    print(i.text)
#根据标签查找
all_elements = wd.find_elements(By.TAG_NAME,'span')
for i in all_elements:
    print(i.text)
#退出
wd.quit()

很简单和第一个差不多,就是变成了,找一个(element),变成了找所有(elements)


----------------------------------三-----------------------------------------------

第三篇代码:

python 复制代码
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

url = 'https://im.qq.com/index/'


wd = webdriver.Edge()
wd.implicitly_wait(10)

wd.get(url)

elements = wd.find_element(By.NAME,'im.qq.com.login')
elements.click()
time.sleep(1)
element =  wd.find_element(By.ID,'bottom_qlogin')
time.sleep(1)
element2 = wd.find_element(By.ID,'switcher_plogin')
time.sleep(1)
element2.click()
time.sleep(2)
wd.quit()

这个也没啥就是-----wd.implicitly_wait(10)------因为爬取要时间,相当一个等待的代码。


------------------------------四-------------------------------

第四篇代码:

frame窗口转换

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

url = "https://cdn2.byhy.net/files/selenium/sample2.html"
wd = webdriver.Edge()
wd.implicitly_wait(10)

wd.get(url)
#切换到内frame里面
wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR,'[src="sample1.html"]'))
elements = wd.find_elements(By.CSS_SELECTOR,'.plant')
for i in elements:
    print(i.get_attribute('outerHTML'))
#切换到外部
wd.switch_to.default_content()
wd.find_element(By.CSS_SELECTOR,'#outerbutton')
print(wd.find_element(By.CSS_SELECTOR,'#outerbutton').get_attribute('outerHTML'))
wd.find_element(By.CSS_SELECTOR,'#outerbutton').click()
time.sleep(2)
wd.quit()
input("jj")

这个很重要,加入了CSS,CSS也就是选择器,很强大。

1.wd.switch_to.frame(wd.find_element(By.CSS_SELECTOR,'src="sample1.html"'))

找么有'ID'或者'Class'的,并且进入frame窗口

2.print(i.get_attribute('outerHTML'))------这个将会打印标签在HTML是什么样的,打印出来就是什么样的

3.wd.switch_to.default_content()------------返回到外部窗口


-------------------------------------------五-------------------------------------------

第五篇代码:

浏览器窗口的变化

python 复制代码
import time
from selenium  import webdriver
from selenium.webdriver.common.by import By

url = "https://cdn2.byhy.net/files/selenium/sample3.html"
wd = webdriver.Edge()
wd.implicitly_wait(10)

wd.get(url)

element = wd.find_element(By.CSS_SELECTOR,'a')

print(element.get_attribute('outerHTML'))
element.click()
#存储下来
mainWindow = wd.current_window_handle

time.sleep(5)

for handle in wd.window_handles:
    wd.switch_to.window(handle)
    print(wd.title)
    if '必应' in wd.title:
        break

wd.find_element(By.CSS_SELECTOR,'#sb_form_q').send_keys("hahahah")
time.sleep(1)
wd.find_element(By.CSS_SELECTOR,'#search_icon').click()

time.sleep(5)
#返回窗口
wd.switch_to.window(mainWindow)

wd.find_element(By.CSS_SELECTOR,'button').click()
wd.find_element(By.CSS_SELECTOR,'button').click()
time.sleep(5)

wd.quit()

1.mainWindow = wd.current_window_handle-这个很有必要,就是存储当前窗口,以便返回。

python 复制代码
for handle in wd.window_handles:
    wd.switch_to.window(handle)
    print(wd.title)
    if '必应' in wd.title:
        break

在目前浏览器窗口找,有必应两个字的窗口。并转到


为什么要写这个博客呢,这个代码前面其实都有,就是结束了。明天搞个qq登录,这个就结束了。

当然也会更新小说爬取,和百度翻译,百度小姐姐的爬取,的对比爬取。总结嘛!!!加油!!!


相关推荐
Y幽谷客2 小时前
Python加载本地大模型(Qwen3.5 8B)
python·大模型
伞伞悦读3 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
线上放牧人3 小时前
Windows删除图标缓存
windows·python·pyqt
GreenTea3 小时前
GrokBot 核心成员 Lauren Tan:每月交付 2000 个 PR 的人,是怎么用 AI 的
前端·后端·架构
qq_5470261793 小时前
Python 变量和简单数据类型
python
mCell4 小时前
用 Knip 清理 AI Coding 留下的冗余代码
前端·ai编程·前端工程化
智搜广告5 小时前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo
IpdataCloud5 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip
笃行3505 小时前
使用Rokid AIUI做了一个童年推箱子游戏
前端
2601_966949655 小时前
只拿到股票代码还不够:量化程序到底还需要哪些标的信息?——从数据建模开始理解股票元数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash