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. Windows 下redis的安装和使用

    1.下载 Window 下载地址:https://github.com/MSOpenTech/redis/releases 查找版本对应的一个MSI或者zip文件下载 2.安装 MSI文件需要安装 z ...

  2. BZOJ3879 SvT(后缀树+虚树)

    对反串建SAM得到后缀树,两后缀的lcp就是其在后缀树上lca的len值,于是每次询问对后缀树建出虚树并统计答案即可. #include<iostream> #include<cst ...

  3. react中jsx文件是如何转换成js对象的

    通过在线babel转换器,转换出jsx是如何变成js对象的 jsx文件 加入了正常的标签以及嵌套标签以及方法属性 function hello() { click=()=>{ console.l ...

  4. 搭建SSM环境(淘淘商城)

    本文用到的资料: 链接:https://pan.baidu.com/s/1Pk_aI_PRbqRFP9i3o9Xodg 提取码:o4o4 1.1. 数据库 1.1.1. 使用navicat创建数据库连 ...

  5. 数据库-SQL语句练习【已完成26题,还剩35题】

    练习题链接:https://www.nowcoder.com/ta/sql?page=0 错题频次表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16     1+1 1+ ...

  6. JQuery攻略读书笔记---第2章 数组

      2 数组2.8 创建对象数组循环数组2.9 数组排序 2 数组 2.8 创建对象数组 //数组化对象 var student =[ { "role":101, "na ...

  7. 基于PhotoView的头像/圆形裁剪控件

    常见的图片裁剪有两种,一种是图片固定,裁剪框移动放缩来确定裁剪区域,早期见的比较多,缺点在于不能直接预览裁剪后的效果:还有一种现在比较普遍了,就是裁剪框固定,直接拖动缩放图片,便于预览裁剪结果. 我做 ...

  8. Win10安装PyQt5与Qt Designer

    1.直接在cmd中通过pip安装PyQt5 1 pip install pyqt5 会自动下载PyQt5以及sip并安装,因为PyQt5不再提供Qt Designer等工具,所以需要再安装pyqt5- ...

  9. spingboot启动报驱动Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of th

    原因: springboot应用了最新的驱动com.mysql.cj.jdbc.Driver,这个驱动需要用mysql-connector-java包的6.x版本才可以, 而mysql-connect ...

  10. 关于ECharts甘特图的实现

    对于使用ECharts图表的步骤,每种图表都是一致的,相信大家也都了解 此处只分享甘特图的option,代码如下: option: { title: { text: '项目实施进度表', left: ...