爬虫练习: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)

版权声明和免责声明

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

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

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

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

相关推荐
爬虫程序猿28 分钟前
《京东商品详情爬取实战指南》
爬虫·python
ningmengjing_3 小时前
webpack打包方式
前端·爬虫·webpack·node.js·逆向
小白学大数据6 小时前
构建企业级Selenium爬虫:基于隧道代理的IP管理架构
爬虫·tcp/ip·selenium
华科云商xiao徐8 小时前
详解Selenium爬虫部署七大常见错误及修复方案
爬虫·selenium
华科云商xiao徐9 小时前
Linux环境下爬虫程序的部署难题与系统性解决方案
爬虫·数据挖掘·数据分析
qq_3129201110 小时前
Nginx限流与防爬虫与安全配置方案
运维·爬虫·nginx·安全
别来无恙14911 小时前
使用Python和Selenium进行Web自动化测试:从入门到实践
selenium·测试工具
Python大数据分析@11 小时前
python用selenium怎么规避检测?
开发语言·python·selenium·网络爬虫
ThreeAu.11 小时前
Miniconda3搭建Selenium的python虚拟环境全攻略
开发语言·python·selenium·minicoda·python环境配置