Python3 使用selenium插件爬取苏宁商家联系电话
作者:菜鸟挣扎史 时间:2023-12-20 01:39:57
Selenium简介
Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome、Firefox、Safari等主流界面浏览器,同时也支持phantomJS * 面浏览器。
此处使用了selenium插件 使用的是火狐浏览器 信息存储到csv表格里面
前面详细不多讲如果条件不满足自行百度安装
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 11 20:21:04 2019
@author: Administrator
"""
from selenium import webdriver
import time
import random
import csv
import codecs
#此处为要爬取的页数默认为 50页
yema = 50
#要爬取的网址 此处网址为搜索详细产品出现的产品搜索结果页
#注意苏宁搜索行业词出现的产品页面是不一样的
wangzhi = "https://search.suning.com/%E4%BC%91%E9%97%B2%E9%A3%9F%E5%93%81/"
#codevs 防止中文写入时乱码
f = codecs.open('suning.csv','a',encoding='utf-8')
csv_writer = csv.writer(f)
#谷歌
#browser = webdriver.chrom.webdirver.WebDriver(executable_path="chromedriver")
#火狐
browser1 = webdriver.Firefox(executable_path="geckodriver")
def browser_1(url,browser=browser1):
#打开网页
browser.get(url)
return(browser)
browser = browser_1(wangzhi)
#通过class找到元素
#input_guanggao = browser.find_element_by_class_name("close-btn")
#点一下
#input_guanggao.click()
#输入
#input_txt.send_keys("111")
#翻页键
#next_page = browser.find_element_by_class_name("next")
#数据提取
urls = []
nub = 1
for i in range(yema-1):
print(i)
#将滚动条拖到底部
js="var q=document.documentElement.scrollTop=100000"
browser.execute_script(js)
time.sleep(random.randint(5,10))
shops = browser.find_elements_by_class_name("sellPoint")
for shop in shops:
#print(shop)
#print("*"*10)
url = shop.get_attribute('href')
vip = "/0000000000/"
if vip not in url:
urls.append(url)
print(i,"--",nub,"--",url)
nub += 1
print(i,"页")
i += 1
js="var q=document.documentElement.scrollTop=500"
browser.execute_script(js)
time.sleep(random.randint(3,5))
next_page = browser.find_element_by_class_name("next")
time.sleep(random.randint(3,5))
next_page.click()
time.sleep(random.randint(5,8))
print("---"*10)
for ul in urls:
browser_shop = browser_1(ul)
#公司名称
chead_companyName = browser_shop.find_element_by_id("chead_companyName")
#电话
chead_telPhone = browser_shop.find_element_by_id("chead_telPhone")
#地址
chead_companyAddress = browser_shop.find_element_by_id("chead_companyAddress")
browser_shop.find_element_by_class_name("storname").click()
#chead_telPhone.find_element_by_xpath("//*[contains(text(),'13816391436')]").click()
companyName = chead_companyName.text
if companyName == "":
companyName = "null"
telPhone = chead_telPhone.text
if telPhone == "":
telPhone = "null"
companyAddress = chead_companyAddress.text
if companyAddress == "":
companyAddress = "null"
print(companyName,"==",telPhone,"==",companyAddress)
csv_writer.writerow([companyName,telPhone,companyAddress])
#browser_shop.close()
f.close()
print("结束")
总结
以上所述是小编给大家介绍的Python3 使用selenium插件爬取苏宁商家联系电话网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/qq_39503451/article/details/103651936
标签:python,selenium
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
VBScript WeekdayName 函数语法
2008-01-25 19:25:00
python爬虫请求头的使用
2023-03-21 07:10:23
python软件都是免费的吗
2022-09-04 03:45:53
golang 实用库gotable的具体使用
2024-04-27 15:31:48
如何基于Python Matplotlib实现网格动画
2023-07-31 04:17:37
![](https://img.aspxhome.com/file/2023/4/115174_0s.gif)
不同情况下修复Access数据库的实用方法
2008-11-28 16:18:00
mcrypt启用 加密以及解密过程详细解析
2023-07-15 19:41:55
Python简单实现gif动图倒放示例
2021-08-20 16:29:21
![](https://img.aspxhome.com/file/2023/6/123516_0s.png)
Windows系统配置python脚本开机启动的3种方法分享
2022-02-27 22:09:24
![](https://img.aspxhome.com/file/2023/5/103775_0s.png)
一文搞懂python可迭代对象,迭代器,生成器,协程
2023-08-22 18:45:13
CentOS下重置MySQL的root密码的教程
2024-01-25 19:20:40
ThinkPHP CURD方法之limit方法详解
2023-11-15 03:58:00
Python Pandas处理CSV文件的常用技巧分享
2022-06-18 12:56:19
![](https://img.aspxhome.com/file/2023/1/99621_0s.png)
Python 权限控制模块 Casbin
2022-09-21 07:35:46
![](https://img.aspxhome.com/file/2023/5/121135_0s.png)
鼠标右击事件代码(asp.net后台)
2024-04-19 10:07:32
SQLyog的下载、安装、破解、配置教程(MySQL可视化工具安装)
2024-01-21 19:53:06
![](https://img.aspxhome.com/file/2023/5/71725_0s.png)
GO CountMinSketch计数器(布隆过滤器思想的近似计数器)
2024-02-17 06:12:40
![](https://img.aspxhome.com/file/2023/3/108093_0s.png)
python 实现图与图之间的间距调整subplots_adjust
2023-10-05 00:52:42
![](https://img.aspxhome.com/file/2023/0/118310_0s.jpg)
深入浅析Vue中mixin和extend的区别和使用场景
2024-05-29 22:42:43
如何安装并使用conda指令管理python环境
2022-07-02 04:08:43