为了解决采购妹子一个个的翻,我将1688网上想找的产品撸了下来,这里以放一个词为代表。代码以及完整的数据结构已上传 https://github.com/jevy146/selenium_1688/

# -*- coding: utf-8 -*-
# @Time    : 2020/6/18 9:31
# @Author  : 结尾!!
# @FileName: D01-抓取首页信息.py
# @Software: PyCharmfrom selenium.webdriver import ChromeOptions
import time
from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait#第一步实现对淘宝的登陆
class Chrome_drive():def __init__(self):ua = UserAgent()option = ChromeOptions()option.add_experimental_option('excludeSwitches', ['enable-automation'])option.add_experimental_option('useAutomationExtension', False)NoImage = {"profile.managed_default_content_settings.images": 2}  # 控制 没有图片option.add_experimental_option("prefs", NoImage)# option.add_argument(f'user-agent={ua.chrome}')  # 增加浏览器头部# chrome_options.add_argument(f"--proxy-server=http://{self.ip}")  # 增加IP地址。。# option.add_argument('--headless')  #无头模式 不弹出浏览器self.browser = webdriver.Chrome(options=option)self.browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': 'Object.defineProperty(navigator,"webdriver",{get:()=>undefined})'})  #去掉selenium的驱动设置self.browser.set_window_size(1200,768)self.wait = WebDriverWait(self.browser, 12)def get_login(self):url='https://www.1688.com/'self.browser.get(url)#self.browser.maximize_window()  # 在这里登陆的中国大陆的邮编#这里进行人工登陆。time.sleep(2)self.browser.refresh()  # 刷新方法 refresreturn#获取判断网页文本的内容:def index_page(self,page,wd):"""抓取索引页:param page: 页码"""print('正在爬取第', page, '页')url = f'https://s.1688.com/selloffer/offer_search.htm?keywords=%D0%A1%D0%CD%C3%AB%BD%ED%BC%D3%C8%C8%B9%F1&n=y&netType=16&beginPage={page}#sm-filtbar'js1 = f" window.open('{url}')"  # 执行打开新的标签页print(url)self.browser.execute_script(js1)  # 打开新的网页标签# 执行打开新一个标签页。self.browser.switch_to.window(self.browser.window_handles[-1])  # 此行代码用来定位当前页面窗口self.buffer()  # 网页滑动  成功切换#等待元素加载出来time.sleep(3)self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#render-engine-page-container > div > div.common-pagination > div > div > div > span:nth-child(2) > input')))#获取网页的源代码html =  self.browser.page_sourceget_products(wd,html)self.close_window()def buffer(self): #滑动网页的for i in range(20):time.sleep(0.5)self.browser.execute_script('window.scrollBy(0,380)', '')  # 向下滑行300像素。def close_window(self):length=self.browser.window_handlesprint('length',length) #判断当前网页窗口的数量if  len(length) > 3:self.browser.switch_to.window(self.browser.window_handles[1])self.browser.close()time.sleep(1)self.browser.switch_to.window(self.browser.window_handles[-1])import csv
def save_csv(lise_line):file = csv.writer(open("./1688_com.csv",'a',newline="",encoding="utf-8"))file.writerow(lise_line)#解析网页,
from scrapy.selector import Selector
def get_products(wd,html_text):"""提取商品数据"""select=Selector(text=html_text)# 大概有47个items = select.xpath('//*[@id="sm-offer-list"]/div/*').extract()print('产品数 ',len(items))for i in range(1, len(items)+1):#详情页链接desc_href = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="img-container"]//a/@href').extract_first()# 图片链接img_url  = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="img"]/@style').extract_first()# 复购率shop_repurchase_rate = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="desc-container"]//span[@class="shop-repurchase-rate"]/text()').extract_first()# title  # 标题title = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="desc-container"]//div[@class="title"]//text()').extract()title_name=''.join(title)#price  #价格price = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="desc-container"]//div[@class="price-container"]/div[@class="price"]/text()').extract_first()# sales_num  # 成交量sales_num = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="desc-container"]//div[@class="price-container"]/div[@class="sale"]/text()').extract_first()#company_name  # 公司名称company_name = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="company-name"]/a/text()').extract_first()#company_href  # 公司链接company_href = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="company-name"]/a/@href').extract_first()#company_tag  # 公司标签company_tag = select.xpath(f'//*[@id="sm-offer-list"]/div[{i}]//div[@class="common-company-tag"]//text()').extract_first()all_desc=[wd,title_name,img_url,desc_href,price,sales_num,company_name,company_href,company_tag,shop_repurchase_rate]print(all_desc)save_csv(all_desc)def main():"""遍历每一页"""run=Chrome_drive()run.get_login() #先扫码登录wd=['小型毛巾加热柜']for w in wd:for i in range(1, 6): #1688总计展示了6页,抓取了前5页的内容run.index_page(i,w)if __name__ == '__main__':csv_head = 'word,title_name,img_url,desc_href,price,sales_num,company_name,company_href,company_tag,shop_repurchase_rate'.split(',')save_csv(csv_head)main()

运行过程

3.运行结束 抓了5页,一页60个,没有遗漏的

使用selenium抓取1688供应商相关推荐

  1. selenium抓取_使用Selenium的网络抓取电子商务网站

    selenium抓取 In this article we will go through a web scraping process of an E-Commerce website. I hav ...

  2. Python爬虫实战八之利用Selenium抓取淘宝匿名旺旺

    其实本文的初衷是为了获取淘宝的非匿名旺旺,在淘宝详情页的最下方有相关评论,含有非匿名旺旺号,快一年了淘宝都没有修复这个. 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语 ...

  3. [Python] python + selenium 抓取 京东商品数据(商品名称,售价,店铺,分类,订单信息,好评率,评论等)

    目录 一.环境 二.简介 三.京东网页分析 1.获取商品信息入口--商品列表链接获取 2.获取商品信息入口--商品详情链接获取 3.商品详情获取 4.商品评论获取 四.代码实现 五.运行结果 六.结语 ...

  4. selenium抓取动态网页数据

    1.selenium抓取动态网页数据基础介绍 1.1 什么是AJAX AJAX(Asynchronouse JavaScript And XML:异步JavaScript和XML)通过在后台与服务器进 ...

  5. python selenium 处理弹窗_python+selenium 抓取弹出对话框信息

    抓取弹出对话框信息,困挠了我很久,我百度了很久,一直没有找到我想要的内容.最近学习到了. 有两种方法: 1.driver.switch_to.alert.text 2.result = EC.aler ...

  6. Python爬虫用Selenium抓取js生成的文件(一)

    简介 任务简述 实现过程 简介 我最近在看关于计算机的一些书籍,发现了这个电子书清单:计算机开放电子书汇总, 和大家分享一下. 我在下载其中的书籍时被导向了这个很好的计算机电子书网站KanCloud看 ...

  7. python 弹出对话框_python+selenium 抓取弹出对话框信息

    抓取弹出对话框信息,困挠了我很久,我百度了很久,一直没有找到我想要的内容.最近学习到了. 有两种方法: 1.driver.switch_to.alert.text 2.result = EC.aler ...

  8. python爬携程_用python selenium抓取携程信息

    最近在学习selenium,遇到一个很奇怪的问题,debug了半天还是没弄明白,我是在测试抓取携程网站的机票信息 我的代码: # -*- coding: utf-8 -*- from selenium ...

  9. python爬取豆瓣读书_用python+selenium抓取豆瓣读书中最受关注图书并按照评分排序...

    抓取豆瓣读书中的(http://book.douban.com/)最受关注图书,按照评分排序,并保存至txt文件中,需要抓取书籍的名称,作者,评分,体裁和一句话评论 方法一:#coding=utf-8 ...

最新文章

  1. 【Android NDK 开发】JNI 线程 ( JNI 线程创建 | 线程执行函数 | 非 JNI 方法获取 JNIEnv 与 Java 对象 | 线程获取 JNIEnv | 全局变量设置 )
  2. 数据结构:Binary and other trees(数据结构,算法及应用(C++叙事描述语言)文章8章)...
  3. python psutils
  4. 【Linux】Linux下建立和管理逻辑卷
  5. android 图片缓存工具类,Android工具类系列-Glide图片缓存与圆角
  6. c#编程指南(四) 组元(Tuple)
  7. Stack Overflow RToax
  8. 浅谈堆栈问题-C++
  9. 数据结构学习(1):单链表
  10. Android 11 存储权限适配指南
  11. 三极管的下拉电阻作用是什么?
  12. python实现华氏温度和摄氏温度转换
  13. 移植Python3到TQ2440(二)
  14. import mysql data to solr4.2.0
  15. java实现多个excel表格数据整合成一个excel表格
  16. 82ip网 - 每日更新50万HTTP和HTTPS代理IP!
  17. 惠普关闭 secure boot
  18. 阿里云大学安全课程-阿里云首席安全研究员吴瀚清:WannaCry事件最“细思恐极“的一个事实是?...
  19. select()函数详解
  20. 造梦师的梦想是什么样的?

热门文章

  1. 年薪20万招java讲师
  2. word2vec关键词提取 python_如何从word2vec的Google预训练模型中提取单词向量?
  3. 2020年MOOCC语言程序设计精髓第十四周编程题练兵
  4. c++继承---私有继承
  5. win10更新后低分辨率显卡驱动丢失,NVIDIA图形驱动程序安装失败终极解决方法
  6. 百度android sdk聚合,SDK接入 · 百度移动统计Android SDK使用手册
  7. C++课程设计——健身俱乐部管理
  8. MNS合约计划重磅而来
  9. 激发学生学计算机的兴趣,[在计算机教学中如何激发学生的学习兴趣]
  10. SMPTE ST 2110 概论(二)