selenium爬去数据+存储
1 爬去数据代码
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#加载TimeoutException模块,用于进行超时处理
from selenium.common.exceptions import TimeoutException
#正则表达式
import re,sys
from pyquery import PyQuery as pq
from config import *
#加载数据库操作模块
import mysqlOp driver=webdriver.Chrome()
#使用phantomJs浏览器驱动
#driver=webdriver.PhantomJS()
driver.get("https://www.taobao.com")
driver.set_window_size(1400,900) wait=WebDriverWait(driver, 10)
def search():
try:
input=wait.until(EC.presence_of_element_located(By.CSS_SELECTOR,"#q"))
submit=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#J_TSearchForm > div.search-button > button")))
input.clear()
input.send_keys("美食")
submit.click()
#获取第一页的数据
get_goods()
except TimeoutException :
search()
#获取总页码
def get_total():
#查找总页码
total=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > div.total")))
return total.text
#翻页
def next_page(page):
try:
input=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#mainsrp-pager > div > div > div > div.form > input")))
submit=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit")))
input.clear()
input.send_keys(page)
submit.click()
wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR,"#mainsrp-pager > div > div > div > ul > li.item.active > span"),str(page))) #获取当前页的数据
count=get_goods()
except TimeoutException:
next_page(page)
return count
def get_goods():
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"#mainsrp-itemlist .items .item")))
#mainsrp-itemlist > div > div > div:nth-child(1) > div.item.J_MouserOnverReq.item-ad
#mainsrp-itemlist > div > div > div:nth-child(1)
html=driver.page_source
doc=pq(html)
items=doc("#mainsrp-itemlist .items .item").items()
count=0
for item in items:
goods={
'image':item.find('.pic .img').attr('src'),
'price':item.find('.price').text(),
'deal' :item.find('.deal-cnt').text()[:-3],
'title':item.find('.title').text(),
'shop':item.find('.shop').text(),
'location':item.find('.location').text() }
print(goods)
#将数据插入数据库
mysqlOp.mysqlOp(goods)
count+=1
return count
def main():
search()
total=get_total()
#使用正则表达式提取页码
total=int(re.compile(r"(\d+)").search(total).group(1))
print(total)
total_count=0
for i in range(2,total+1):
count=next_page(i)
total_count +=count
print(total_count) if __name__=="__main__":
main()
2 存入到mysql中
创建一个mysqlOp.py的文件
#coding=utf-8
from pymysql import *
def mysqlOp(goods):
conn=connect(host='127.0.0.1', port=3306, user='root', passwd='1qaz2wsx#EDC', db='taobao_meishi', charset='utf8')
cursor=conn.cursor()
cursor.execute("insert into goods(image,price,deal,title,shop,location) values(%s,%s,%s,%s,%s,%s)",(goods['image'],goods['price'],goods['deal'],goods['title'],goods['shop'],goods['location']))
conn.commit()
cursor.close()
conn.close()
selenium爬去数据+存储的更多相关文章
- (完整)爬取数据存储之TXT、JSON、CSV存储
一.文件存储 1. TXT文本存储 例:知乎发现页面,获得数据存成TXT文本 import requests from pyquery import PyQuery as pq url="h ...
- 学习爬虫的day03 (通过代理去爬去数据)
代理的IP通过去网上找# -*- coding: utf-8 -*- import re import _thread from time import sleep, ctime from urlli ...
- scrapy使用PhantomJS和selenium爬取数据
1.phantomjs 安装 下载:http://phantomjs.org/download.html 解压: tar -jxvf phantomjs--linux-x86_64.tar.bz2 重 ...
- 利用Selenium爬取淘宝商品信息
一. Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...
- R中使用rvest爬取数据小试
总结R中使用 xpath 和 css selectors 获取标签内容(xpath功能强大,而CSS选择器通常语法比较简洁,运行速度更快些) 例:抓取下面标签的内容: <h3 class=&qu ...
- python3下scrapy爬虫(第九卷:scrapy数据存储进JSON文件)
将爬取数据存储在JSON文件里并不难,只需修改pipelines文件 直接看代码: 来看下结果: 中文字符恶心的很 之后我会在后卷中做出修改
- 使用Selenium爬取网站表格类数据
本文转载自一下网站:Python爬虫(5):Selenium 爬取东方财富网股票财务报表 https://www.makcyun.top/web_scraping_withpython5.html 需 ...
- 利用selenium 爬取豆瓣 武林外传数据并且完成 数据可视化 情绪分析
全文的步骤可以大概分为几步: 一:数据获取,利用selenium+多进程(linux上selenium 多进程可能会有问题)+kafka写数据(linux首选必选耦合)windows直接采用的是写my ...
- 使用selenium爬取网站动态数据
处理页面动态加载的爬取 selenium selenium是python的一个第三方库,可以实现让浏览器完成自动化的操作,比如说点击按钮拖动滚轮等 环境搭建: 安装:pip install selen ...
随机推荐
- Linux 防SSH暴力攻击
在下这几天发现我的VPS 总是莫名遭受到 江苏镇江那边的IP 登录请求攻击 ,跟踪了下路由,发现ip是从蒙古那边出去的,然后意识到可能是有扫描端口的.. 方法一: 现在的互联网非常不安全,很多人没事就 ...
- 五种编程语言解释数据结构与算法——顺序表2(java与C++语言实现)
5.java实现方式: 5.1.顺序表的抽象结构 package com.xgp.顺序表; public interface MyList<T> { //1. initList(& ...
- IIS WEB站点设置
IIS安装 打开控制面板 -> 程序 -> 打开或关闭Windows功能 ,在弹出得对话框中选择“Internet信息服务”复选框.我这里是Windows server 2019 ,界面有 ...
- PostgreSQL内核学习笔记十一(索引)
Index Scan涉及到两部分的内容Heap Only Tuple和index-only-scan. 什么是Heap Only Tuple(HOT)? 例如:Update a Row Without ...
- set()和get()方法
在很多程序中,都喜欢定义一个privata变量,然后为这个私有变量加上get(),set()方法.那为什么不直接定义一个public变量呢?这样做到底有什么好处和意义呢?难道真的仅仅只是为了代码规范? ...
- UI设计的定义和view、viewgroup及其一些常用属性
1.uI设计 UI设计就是用户界面设计,比如QQ登录界面.聊天界面等,进行UI设计将会用到View和Viewgroup. 2.view 在安卓中可以理解为视图,在安卓中占用一块矩形区域,负责提供主件绘 ...
- cf1041E
题意:要求你构造一棵树,树中每一个节点都有一个编号(互不相同),告诉你删除掉每一条边之后的两个联通分量中节点标号的最大值,要求你输出这颗树,不存在就输出NO 题解:可以发现这颗树实际上是一个序列,我们 ...
- mysql - 拼接多个字段
方法介绍 concat_ws(分隔符,需要拼接在一起的字段) 实例 SELECT day_catering.S_ID, day_catering.S_DAY_WEEKS_CAPTION, concat ...
- tensorflow张量限幅
本篇内容有clip_by_value.clip_by_norm.gradient clipping 1.tf.clip_by_value a = tf.range(10) print(a) # if ...
- Connections in Galaxy War ZOJ - 3261 离线操作+逆序并查集 并查集删边
#include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ...