爬虫案例实战

文章目录


一、窗口切换实战

案例实战:使用selenium实现打开百度和腾讯两个窗口并切换

知识点:用到selenium中execute_script()执行js代码及switch_to.window()方法

全部代码如下:

python 复制代码
import time
import warnings
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options


warnings.filterwarnings('ignore')
# 创建ChromeOptions对象
chrome_options = Options()
# 添加启动参数,禁用浏览器自动化控制提示
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

driver = webdriver.Chrome(chrome_options)
driver.execute_cdp_cmd(
    "Page.addScriptToEvaluateOnNewDocument",
    {
        "source": " Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) "
    }
)
driver.maximize_window()
# 请求第1个网址:driver.get()
driver.get('https://www.baidu.com')
time.sleep(3)

# 请求第2个网址:JS代码
js_code = 'window.open("https://www.qq.com")'
driver.execute_script(js_code)

# 获取窗口
window_list = driver.window_handles

# 切换窗口到百度
driver.switch_to.window(window_list[0])
driver.find_element(by=By.ID, value='kw').send_keys('风景')
driver.find_element(by=By.ID, value='su').click()
time.sleep(2)

# 切换窗口到QQ
driver.switch_to.window(window_list[1])
driver.find_element(by=By.XPATH, value='//*[@id="qqhome-top-header"]/div/div/div[2]/div/input').send_keys('Python')
driver.find_element(by=By.XPATH, value='//*[@id="qqhome-top-header"]/div/div/div[2]/div/button/span').click()
input()

二、京东数据抓取

案例实战:使用selenium实现打开京东搜索商品并滑动页面获取数据

知识点:用到selenium中execute_script()执行js代码

全部代码如下:

python 复制代码
import time
import warnings
from lxml import etree
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

keyword = '手机'
warnings.filterwarnings('ignore')
# 创建ChromeOptions对象
chrome_options = Options()
# 添加启动参数,禁用浏览器自动化控制提示
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

driver = webdriver.Chrome(chrome_options)
driver.execute_cdp_cmd(
    "Page.addScriptToEvaluateOnNewDocument",
    {
        "source": " Object.defineProperty(navigator, 'webdriver', { get: () => undefined }) "
    }
)
driver.maximize_window()


def requests_url():
    # 访问京东首页
    start_url = r'https://www.jd.com/'
    driver.get(start_url)
    input_data()


def input_data():
    # 输入数据
    driver.find_element(by=By.ID, value='key').send_keys(keyword)
    time.sleep(2)
    driver.find_element(by=By.CLASS_NAME, value='button').click()
    time.sleep(8)
    Down_Scroll()


def Down_Scroll():
    # 控制鼠标滑到底部
    for i in range(1, 11):
        js_code = 'scrollTo(0, {})'.format(i * 600)
        driver.execute_script(js_code)
        time.sleep(1)
    time.sleep(3)
    get_goods_info()


def get_goods_info():
    # 解析响应
    response = driver.page_source
    html_xpath = etree.HTML(response)
    li_list = html_xpath.xpath('//div[@id="J_goodsList"]/ul/li')
    for li in li_list:
        # 1、商品标题
        goods_name_1 = li.xpath(r'.//div[@class="p-name p-name-type-2"]/a/em/text()')
        goods_name_2 = li.xpath(r'.//div[@class="p-name p-name-type-2"]/a/@title')
        goods_name = goods_name_1 if goods_name_1 != [] else goods_name_2
        goods_name = ''.join(goods_name).replace('\n', '').replace(' ', '').replace('	', '')
        # 2、卖家名字
        sale_name = li.xpath(r'.//a[@class="curr-shop hd-shopname"]/@title')
        sale_name = ''.join(sale_name)
        # 3、商品价格
        goods_price = li.xpath(r'.//div/div[2]/strong/i/text()')
        goods_price = ''.join(goods_price)
        if goods_name != '':
            print(goods_name, sale_name, goods_price, sep=' | ')


def main():
    requests_url()
    input()


if __name__ == '__main__':
    main()
相关推荐
DogDaoDao42 分钟前
用PyTorch实现多类图像分类:从原理到实际操作
图像处理·人工智能·pytorch·python·深度学习·分类·图像分类
默归1 小时前
分治法——二分答案
python·算法
麻雀无能为力2 小时前
python自学笔记14 NumPy 线性代数
笔记·python·numpy
大学生毕业题目2 小时前
毕业项目推荐:28-基于yolov8/yolov5/yolo11的电塔危险物品检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·cnn·pyqt·电塔·危险物品
程序猿小D4 小时前
【完整源码+数据集+部署教程】脑部CT图像分割系统源码和数据集:改进yolo11-CSwinTransformer
python·yolo·计算机视觉·数据集·yolo11·脑部ct图像分割
max5006004 小时前
北京大学MuMo多模态肿瘤分类模型复现与迁移学习
人工智能·python·机器学习·分类·数据挖掘·迁移学习
修一呀5 小时前
[后端快速搭建]基于 Django+DeepSeek API 快速搭建智能问答后端
后端·python·django
WSSWWWSSW5 小时前
Seaborn数据可视化实战:Seaborn数据可视化实战入门
python·信息可视化·数据挖掘·数据分析·matplotlib·seaborn
小石5 小时前
Python 装饰器核心知识点:无参装饰器构建、带参装饰器扩展及函数与类实现差异
python
巴厘猫6 小时前
从 Manim 中提取表格 / 坐标系并转 GIF:实用方案与核心代码
python·音视频开发