1.re实现
 import requests
from requests.exceptions import RequestException
import re,json
import xlwt,xlrd # 数据
DATA = []
KEYWORD = 'python'
HEADERS = {'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome'\
'/63.0.3239.132 Safari/537.36'}
MAX_PAGE = 10 def get_target(data_list):
for item in data_list:
temp = {
'title': item['title'],
'price': item['view_price'],
'sales': item['view_sales'],
'isTmall': '否' if float(item['view_fee']) else '是',
'area': item['item_loc'],
'name': item['nick'],
'url': item['detail_url']
}
DATA.append(temp)
return True # 发送http请求,获取网页源码
def get_html(url,*args):
try:
if not args:
response = requests.get(url,headers=HEADERS)
global COOKIES
COOKIES = response.cookies # 获取cookie
else:
response = requests.get(url,headers=HEADERS,cookies=COOKIES) response.encoding = response.apparent_encoding
return response.text
except RequestException:
print('请求源码出错!') # 解析源码,得到目标信息
def parse_html(html,*args):
if not args:
pattern = re.compile(r'g_page_config = (.*?)g_srp_loadCss',re.S)
# 去掉末尾的';'
result = re.findall(pattern, html)[0].strip()[:-1]
# 格式化json,可以用json在线解析工具查看结构
content = json.loads(result)
data_list = content['mods']['itemlist']['data']['auctions']
else:
pattern = re.compile(r'{.*}',re.S)
result = re.findall(pattern,html)[0]
content = json.loads(result)
data_list = content['API.CustomizedApi']['itemlist']['auctions'] get_target(data_list) def save_to_excel():
f_name = '淘宝%s数据'%KEYWORD
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
sheet = book.add_sheet(f_name)
sheet.write(0, 0, 'title')
sheet.write(0, 1, 'price')
sheet.write(0, 2, 'sales')
sheet.write(0, 3, 'isTmall')
sheet.write(0, 4, 'area')
sheet.write(0, 5, 'name')
sheet.write(0, 6, 'url')
for i in range(len(DATA)):
sheet.write(i+1, 0, DATA[i]['title'])
sheet.write(i+1, 1, DATA[i]['price'])
sheet.write(i+1, 2, DATA[i]['sales'])
sheet.write(i+1, 3, DATA[i]['isTmall'])
sheet.write(i+1, 4, DATA[i]['area'])
sheet.write(i+1, 5, DATA[i]['name'])
sheet.write(i+1, 6, DATA[i]['url'])
book.save('淘宝%s数据.xls'%KEYWORD) def main():
for offset in range(MAX_PAGE):
# 首页有12条异步加载的数据 api?
if offset == 0:
url1 = 'https://s.taobao.com/search?q={}&s={}'.format(KEYWORD,offset*44)
html = get_html(url1)
contents = parse_html(html) url2 = 'https://s.taobao.com/api?_ksTS=1532524504679_226&callback=jsonp227&ajax=true&m=customized&' \
'stats_click=search_radio_all:1&q={}'.format(KEYWORD)
html = get_html(url2,2)
contents = parse_html(html,2)
else:
url = 'https://s.taobao.com/search?q={}&s={}'.format(KEYWORD,offset*44)
html = get_html(url)
contents = parse_html(html) save_to_excel()
print(len(DATA)) if __name__ == '__main__':
main()

爬取淘宝商品数据并保存在excel中的更多相关文章

  1. scrapy+selenium 爬取淘宝商城商品数据存入到mongo中

    1.配置信息 # 设置mongo参数 MONGO_URI = 'localhost' MONGO_DB = 'taobao' # 设置搜索关键字 KEYWORDS=['小米手机','华为手机'] # ...

  2. 使用requests、BeautifulSoup、线程池爬取艺龙酒店信息并保存到Excel中

    import requests import time, random, csv from fake_useragent import UserAgent from bs4 import Beauti ...

  3. 使用requests、re、BeautifulSoup、线程池爬取携程酒店信息并保存到Excel中

    import requests import json import re import csv import threadpool import time, random from bs4 impo ...

  4. Python爬取猫眼电影100榜并保存到excel表格

    首先我们前期要导入的第三方类库有; 通过猫眼电影100榜的源码可以看到很有规律 如: 亦或者是: 根据规律我们可以得到非贪婪的正则表达式 """<div class ...

  5. 爬取拉勾网所有python职位并保存到excel表格 对象方式

    # 1.把之间案例,使用bs4,正则,xpath,进行数据提取. # 2.爬取拉钩网上的所有python职位. from urllib import request,parse import json ...

  6. Python 爬取淘宝商品数据挖掘分析实战

    Python 爬取淘宝商品数据挖掘分析实战 项目内容 本案例选择>> 商品类目:沙发: 数量:共100页  4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 爬取淘宝商品 ...

  7. Selenium+Chrome/phantomJS模拟浏览器爬取淘宝商品信息

    #使用selenium+Carome/phantomJS模拟浏览器爬取淘宝商品信息 # 思路: # 第一步:利用selenium驱动浏览器,搜索商品信息,得到商品列表 # 第二步:分析商品页数,驱动浏 ...

  8. python3编写网络爬虫16-使用selenium 爬取淘宝商品信息

    一.使用selenium 模拟浏览器操作爬取淘宝商品信息 之前我们已经成功尝试分析Ajax来抓取相关数据,但是并不是所有页面都可以通过分析Ajax来完成抓取.比如,淘宝,它的整个页面数据确实也是通过A ...

  9. Python爬虫,抓取淘宝商品评论内容!

    作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...

随机推荐

  1. 20191011-构建我们公司自己的自动化接口测试框架-Util的htmlreport模块

    生成htmlreport的模块是我在网上随意找的一个版本,主要生成的report包括接口名称,接口url,请求数据,响应数据,断言词,断言结果等 具体的htmlreport代码如下: # -*- en ...

  2. diy操作系统 附录:gcc栈帧开启与关闭

    在gcc命令行参数中可以使用-fno-omit-frame-pointer来开启栈帧的使用,或者使用-fomit-frame-pointer选项来关闭. 然而,也可以针对某一个函数进行配置方法如下,这 ...

  3. 【判环】Perpetuum Mobile

    Perpetuum Mobile 题目描述 The year is 1902. Albert Einstein is working in the patent office in Bern. Many ...

  4. Scratch运动模块——有趣的弹球游戏(一)

    大家好!我是蓝老师,有了前几期Scratch的基础,相信大家早已摩拳擦掌,跃跃欲试了,甚至还有些小伙伴已经编写了非常不错的程序. 学习编程就是这样不断探索.主动思考.解决问题的过程. 本期内容: 课程 ...

  5. 关于spring中配置文件路径的那些事儿

    在项目中我们经常会需要读一些配置文件来获取配置信息,然而对于这些配置文件在项目中存放的位置以及获取这些配置文件的存放路径却经常搞不清楚,自己研究了一下,记录下来以备后用. 测试代码如下 package ...

  6. (二十二)SpringBoot之使用Druid连接池以及SQL监控和spring监控

    一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  7. 【多进程】php实现 master-worker 守护多进程模式

    <?php class Worker{ public static $count = 2; public static function runAll(){ static::runMaster( ...

  8. layui下拉多选formSelects使用方法

    下载formSelects-v4插件(引入formSelects-v4.css和formSelects-v4.js) 下载地址:https://fly.layui.com/extend/formSel ...

  9. JS错误信息类型

    1.SyntaxError 语法错误 ①变量名不规范 // 变量名不规范 var 1 = 1; 未被捕获的语法错误,这个错误是js机制自动抛出来的 意外的数字 // 下面两个是同一种情况 var 1a ...

  10. Oracle 数据 查询 一对多 取最新一条非 0 数据

    主键id     待查字段     日期 1            6             2019/5/1 1            0             2019/5/2 需求: 找出 ...