爬虫练习:Selenium使用案例

一、获取某电商平台商品信息

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

with open('苏宁商品.csv',mode='a',newline='',encoding='utf-8') as f:
    csv_writer = csv.writer(f)
    csv_writer.writerow(['title','price','comment','shop_name','href'])
#打开谷歌浏览器
driver = webdriver.Chrome()
#打开网页
driver.get('https://search.suning.com/%E7%A9%BA%E8%B0%83/')
driver.implicitly_wait(5)


#滚动
driver.execute_script('document.querySelector("body > div.ng-footer > div.ng-s-footer").scrollIntoView()')
time.sleep(1)
driver.execute_script('document.querySelector("body > div.ng-footer > div.ng-s-footer").scrollIntoView()')
#document.querySelector("body > div.ng-footer > div.ng-s-footer")
time.sleep(1)
#提取数据
divs = driver.find_elements(By.CSS_SELECTOR,'.product-box')
for div in divs:
    price = div.find_element(By.CSS_SELECTOR,'.price-box').text
    title = div.find_element(By.CSS_SELECTOR,'.title-selling-point').text
    comment = div.find_element(By.CSS_SELECTOR,'.info-evaluate').text
    shop_name = div.find_element(By.CSS_SELECTOR,'.store-stock').text
    href = div.find_element(By.CSS_SELECTOR,'.info-evaluate a').get_attribute('href')
    print(title,price,comment,shop_name,href)
    with open('苏宁商品.csv', mode='w', newline='', encoding='utf-8') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow([title,price,comment,shop_name,href])

input()

二、获取某网站小说信息

python 复制代码
url ="https://www.00ksw.com/html/3/3804/"
from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
driver = Chrome()
driver.get(url)
# print(driver.page_source)
links = driver.find_elements(By.XPATH,  "//div[@class='ml list']//ul//li//a")
for link in links:
    print(link)

版权声明和免责声明

本博客提供的所有爬虫代码和相关内容(以下简称"内容")仅供参考和学习之用。任何使用或依赖这些内容的风险均由使用者自行承担。我(博客所有者)不对因使用这些内容而产生的任何直接或间接损失承担责任。

严禁将本博客提供的爬虫代码用于任何违法、不道德或侵犯第三方权益的活动。使用者应当遵守所有适用的法律法规,包括但不限于数据保护法、隐私权法和知识产权法。

如果您选择使用本博客的爬虫代码,您应当确保您的使用行为符合所有相关法律法规,并且不会损害任何人的合法权益。在任何情况下,我(博客所有者)均不对您的行为负责。

如果您对本声明有任何疑问,或者需要进一步的澄清,请通过我的联系方式与我联系。

相关推荐
天才测试猿2 小时前
Selenium自动化测试网页加载太慢怎么解决?
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
TlSfoward17 小时前
TLSFOWARD TLS指纹
开发语言·数据库·爬虫·搜索引擎·https·php
深蓝电商API17 小时前
浏览器一次请求到底经历了什么?
爬虫
李宸净21 小时前
Web自动化测试selenium+python
前端·python·selenium
川石课堂软件测试1 天前
安全测试|服务器安全加固方法
服务器·功能测试·测试工具·jmeter·mysql·web安全·单元测试
oh,huoyuyan2 天前
火车采集器同时运行超多任务运行优化方案
爬虫·火车采集器
前进别停留2 天前
selenium基本使用方式
java·selenium
TlSfoward2 天前
爬虫指纹漂移监控与回归测试:JA3/JA4 变化为什么会影响线上验证 TLSFOWARD
数据库·爬虫·网络协议·搜索引擎
Albart5752 天前
Python爬虫请求头伪装后仍被反爬,基于代理池+随机延迟的绕过实战
开发语言·爬虫·python
小白学大数据3 天前
两周完成爬虫技术栈升级:Scrapy 迁移 Crawlo 的路径与取舍
爬虫·python·scrapy