爬取淘宝商品数据并保存在excel中
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中的更多相关文章
- scrapy+selenium 爬取淘宝商城商品数据存入到mongo中
1.配置信息 # 设置mongo参数 MONGO_URI = 'localhost' MONGO_DB = 'taobao' # 设置搜索关键字 KEYWORDS=['小米手机','华为手机'] # ...
- 使用requests、BeautifulSoup、线程池爬取艺龙酒店信息并保存到Excel中
import requests import time, random, csv from fake_useragent import UserAgent from bs4 import Beauti ...
- 使用requests、re、BeautifulSoup、线程池爬取携程酒店信息并保存到Excel中
import requests import json import re import csv import threadpool import time, random from bs4 impo ...
- Python爬取猫眼电影100榜并保存到excel表格
首先我们前期要导入的第三方类库有; 通过猫眼电影100榜的源码可以看到很有规律 如: 亦或者是: 根据规律我们可以得到非贪婪的正则表达式 """<div class ...
- 爬取拉勾网所有python职位并保存到excel表格 对象方式
# 1.把之间案例,使用bs4,正则,xpath,进行数据提取. # 2.爬取拉钩网上的所有python职位. from urllib import request,parse import json ...
- Python 爬取淘宝商品数据挖掘分析实战
Python 爬取淘宝商品数据挖掘分析实战 项目内容 本案例选择>> 商品类目:沙发: 数量:共100页 4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 爬取淘宝商品 ...
- Selenium+Chrome/phantomJS模拟浏览器爬取淘宝商品信息
#使用selenium+Carome/phantomJS模拟浏览器爬取淘宝商品信息 # 思路: # 第一步:利用selenium驱动浏览器,搜索商品信息,得到商品列表 # 第二步:分析商品页数,驱动浏 ...
- python3编写网络爬虫16-使用selenium 爬取淘宝商品信息
一.使用selenium 模拟浏览器操作爬取淘宝商品信息 之前我们已经成功尝试分析Ajax来抓取相关数据,但是并不是所有页面都可以通过分析Ajax来完成抓取.比如,淘宝,它的整个页面数据确实也是通过A ...
- Python爬虫,抓取淘宝商品评论内容!
作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...
随机推荐
- Java程序员必会常用Linux速查手册
目錄 系统服务管理 文件管理 查看日志 压缩与解压 磁盘和网络管理 防火墙 ftp操作 软件的安装与管理 其他 系统服务管理 systemctl 输出系统中各个服务的状态: systemctl lis ...
- Navicat连接Oracle数据库用户权限问题
解决Navicat连接Oracle数据库用户权限问题: 第一步:在cmd窗口运行[sqlplus], 第二步:输入Oracle的用户名和口令连接Oracle数据库, 第三步:执行授权代码,给用户授予D ...
- WUSTOJ 1283: Hamster(Java)
1283: Hamster 参考博客 wust_tanyao的博客 题目 第0个月有1对仓鼠.仓鼠的寿命是M个月,仓鼠成年后每个月生一对仓鼠(一雌一雄),问N个月后有仓鼠多少对.更多内容点此链接 ...
- Yii2.0 RESTful API 之速率限制
Yii2.0 RESTFul API 之速率限制 什么是速率限制? 权威指南翻译过来为限流,为防止滥用,你应该考虑对您的 API 限流. 例如,您可以限制每个用户 10 分钟内最多调用 API 100 ...
- prometheus+grafana监控nginx
被监控机器环境搭建&配置 nginx-module-vts下载: https://github.com/vozlt/nginx-module-vts nginx-module-vts安装 un ...
- 解决django的后台管理界面添加中文内容乱码问题
在使用django migrate功能时,默认数据库的字符集不是utf8. 是latin 1,然后在后台管理model时,不允许有中文字符插入 解决方案: 在使用migrate建库之前先把数据库建立起 ...
- (三)mybatis之通过接口加载映射配置文件
1.1 需求 通过(二)在全局配置文件 mybatis-configuration.xml 通过 <mappers> 标签来加载映射文件,那么如果我们项目足够大,有很多映射文件呢,难道我 ...
- Docker启动Elasticsearch报错java.nio.file.AccessDeniedException
报错信息 Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes 问题分析 表面上是说容 ...
- Vue路由参数
vue路由参数 1.参数router-link vue.prototype.xxx = {add:fn}`所有组件中,使用this.xxx就能拿到这个对象2.查询字符串 (1)配置: :to=&quo ...
- Oracle11g数据库导入Oracle10g操作成功
转自:https://wenku.baidu.com/view/1b652b57f7ec4afe04a1dfb8.html