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 ...
随机推荐
- 全面了解Java中的15种锁概念及机制!
在读很多并发文章中,会提及各种各样锁如公平锁,乐观锁等等,这篇文章介绍各种锁的分类.介绍的内容如下: 1.公平锁 / 非公平锁 2.可重入锁 / 不可重入锁 3.独享锁 / 共享锁 4.互斥锁 / 读 ...
- python3-cookbook笔记:第六章 数据编码和处理
python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...
- day 15 内置函数
内置函数 不用def定义能直接用的函数,带括号的 locals() # 返回本地作用域中的所有名字 globals() # 返回全局作用域中的所有名字 global 变量 nonlocal 变量 迭代 ...
- KMP刷题记录
[BZOJ4698][SDOI2008]Sandy的卡片 差分一下然后选一个串,用这个串的所有前缀和其他串kmp,求出最长的公共部分即可 代码: #include <bits/stdc++.h& ...
- 【笔记】机器学习 - 李宏毅 - 8 - Backpropagation
反向传播 反向传播主要用到是链式法则. 概念: 损失函数Loss Function是定义在单个训练样本上的,也就是一个样本的误差. 代价函数Cost Function是定义在整个训练集上的,也就是所有 ...
- mysql引擎介绍
mysql存储引擎介绍: 插拔式的插件方式 存储引擎是指定在表上的,即一个库中的每一个表都可以指定专用的存储引擎 不管采用什么样的存储引擎,都会在数据区产生对应的一个frm文件(表结构定义描述文件) ...
- 预防XSs和sql注入常见分析
SQL注入简介SQL 注入漏洞(SQL Injection)是 Web 开发中最常见的一种安全漏洞.可以用它来从数据库获取敏感信息,或者利用数据库的特性执行添加用户,导出文件等一系列恶意操作,甚至有可 ...
- Java开学测试-学生成绩管理系统
题目: 1.定义 ScoreInformation 类,其中包括七个私有变量(stunumber, name, mathematicsscore, englishiscore,networkscore ...
- HDU5608
题意 英文 做法 设\(g(n)=n^2-3n+2\),有\(g(n)=\sum\limits_{d|n}f(d)\),反演一下有\(f(n)=\sum\limits_{d|n}\mu(\frac{n ...
- [ERR] Node goodsleep.vip:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
解决方案 以前的cluster节点信息 保留 要删除 dump.rdb node.conf集群启动时自动生成文件