python爬虫基础之一(爬淘宝)
没想到python如此强大,
今天看一会视频学会了一段python爬虫
这就是我今天学到的内容爬去淘宝网关于书包的一些信息,包括价格,
#coding=utf-8
import requests#导入requests模块
import re#导入re模块
#提取网页代码通用表达式
def getHTMLText(url):
try:
r = requests.get(url,timeout=30)#获取页面的url链接
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
#提取Html中的主要信息
def parseHtml(ilt,html):
try:#下面是正则表达式处理文字信息
plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
flt = re.findall(r'\"raw_title\"\:\".*?\"',html)
for i in range(len(plt)):
price = eval(plt[i].split(":")[1])#eval 可以将前后的”“去掉
name = eval(flt[i].split(":")[1])#split 可以分割字符串到列表红
ilt.append([price,name])
except:
print("")
#将格式打印出来
def printGoodsPrice(ilt):
try:#让排版更清晰
tplt = "{:4}\t{:8}\t{:16}"
print(tplt.format("序号","价格","商品名称"))
count = 0;
for g in ilt:
count = count+1
print(tplt.format(count,g[0],g[1]))
except:
print("")
def main():
goods = '书包'#提取的商品
depth = 2 #提取的深度
url = 'https://s.taobao.com/search?q='+goods
ilt = []
i = 0
for i in range(depth):
try:
irl = url + '&s=' + str(i*44)#这个地方的改动可以影响提取内容
html = getHTMLText(irl)
parseHtml(ilt,html)
except:
continue
printGoodsPrice(ilt)#打印处理
main()
python爬虫基础之一(爬淘宝)的更多相关文章
- python爬虫-基础入门-爬取整个网站《3》
python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...
- python爬虫-基础入门-爬取整个网站《2》
python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...
- python爬虫-基础入门-爬取整个网站《1》
python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...
- Python爬虫之定时抢购淘宝商品
Python爬虫之定时抢购淘宝商品 import time from selenium import webdriver import datetime class Spider: def __ini ...
- python爬虫基础应用----爬取校花网视频
一.爬虫简单介绍 爬虫是什么? 爬虫是首先使用模拟浏览器访问网站获取数据,然后通过解析过滤获得有价值的信息,最后保存到到自己库中的程序. 爬虫程序包括哪些模块? python中的爬虫程序主要包括,re ...
- Python爬虫基础--分布式爬取贝壳网房屋信息(Client)
1. client_code01 2. client_code02 3. 这个时候运行多个client就可以分布式进行数据爬取.
- Python爬虫基础--分布式爬取贝壳网房屋信息(Server)
1. server_code01 2. server_code02 3. server_code03
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- Python爬虫基础
前言 Python非常适合用来开发网页爬虫,理由如下: 1.抓取网页本身的接口 相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简洁:相比其他动态脚本语言,如perl ...
- python爬虫-基础入门-python爬虫突破封锁
python爬虫-基础入门-python爬虫突破封锁 >> 相关概念 >> request概念:是从客户端向服务器发出请求,包括用户提交的信息及客户端的一些信息.客户端可通过H ...
随机推荐
- NHibernate学习过程笔记
NHbernate自动生成数据库的方法: using NHibernate; using NHibernate.Tool.hbm2ddl; namespace Test { public class ...
- 【前端框架-Vue-基础】$attr及$listeners实现跨多级组件的通信
父子 A 组件与 B 组件之间的通信: (父子组件) 如上图所示,A.B.C三个组件依次嵌套,按照 Vue 的开发习惯,父子组件通信可以通过以下方式实现: A to B 通过props的方式向子组件传 ...
- 【2017002】C#FTP上传文件
//上传文件 public static Boolean FtpUpload(string ftpPath, string localFile, FtpServer svr) { //检查目录是否存在 ...
- netstat命令的用法
netstat用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.利用netstat指令可让你得知整个Linux系统的网络情况.参数:-a或–all 显示 ...
- ABAP术语-RFC (Remote Function Call)
RFC (Remote Function Call) 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/12/1101581.html RFC ...
- MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述 # Time: :: # User@Host: **[**] @ [**] Id: ** # Killed: # Query_time: Rows_examined: Rows_affect ...
- mysql alter 增加修改表结构及约束
1) 加索引,添加时若未指定索引名,默认为字段名 mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]); 例子: mysql> alt ...
- 谷歌浏览器添加flash白名单
69以前的版本: 打开 chrome://settings/content/flash 上图中应该有一个"添加"选项 , 依次输入: *.]com [*.]net [*.]org ...
- Action与Func 用法
//vs2017 + framework4.6.2 //zip https://github.com/chxl800/ActionFuncDemo //源文件git https://gith ...
- js 校验身份证号
根据地区编码.身份证格式.18位身份证需要验证最后一位校验位 //校验身份证 function IdentityCodeValid(code) { var city = { 11: "北京& ...