作者:小新 | 来源:互联网 | 2023-02-13 20:13
这个作业属于哪个课程https:edu.cnblogs.comcampusfzzcxyZhichengSoftengineeringPracticeFclass这个作业要求在哪里h
这个作业属于哪个课程 |
https://edu.cnblogs.com/campus/fzzcxy/ZhichengSoftengineeringPracticeFclass |
---|
这个作业要求在哪里 |
https://edu.cnblogs.com/campus/fzzcxy/ZhichengSoftengineeringPracticeFclass/homework/12532 |
这个作业的目标 |
学会使用fiddler工具、git的使用、以及python下requests包的使用 |
Gitee 地址 |
https://gitee.com/leiwjie/lwj212106766/tree/master/demo1 |
一、使用 fiddler 抓包工具+代码,实时监控朴朴上某产品的详细价格信息
(1)解题思路
1、安装Fiddler
2、在csdn寻找Fiddler的使用资料
https://blog.csdn.net/szgyunyun/article/details/104605149?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164726486716780261955716%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164726486716780261955716&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2
3、通过各种尝试后决定用mac端微信打开朴朴小程序
![](https://img2.php1.cn/3cdc5/3ecd/ae9/63bbbff94f482161.png)
4、启动Fiddler对朴朴商品进行抓包
5、解析包数据拿到想要的json内容
![](https://img2.php1.cn/3cdc5/3ecd/ae9/c3ef2b982d505a33.png)
- 6、找到目标地址使用python爬虫进行抓取和数据清洗
(2)设计实现过程
- 1.尝试使用目标地址进行访问连接失败
- 2.去csdn寻找原因最后添加了请求头user-agent连接成功
![](https://img2.php1.cn/3cdc5/3ecd/ae9/2ac41cedc2e7c704.png)
- 3.使用requests包请求数据,将放回的json数据转为字典方便数据提取
![](https://img2.php1.cn/3cdc5/3ecd/ae9/577641b5c5f41010.png)
- 4.使用time.sleep随机每1分钟抓取一次
- 5.进行数据展示处理
- 6.将请求json数据代码快和延时执行抓取价格分别写进函数
#请求网页
def t1():
#发送请求
response_1=requests.get(url, headers=headers)
#设置编码
response_1.encoding='utf-8'
#获取内容
c=response_1.text
#转换成字典
dict=json.loads(c)
data=dict.get('data')
#商品名
name=data.get('name')
#商品价格
price=int_to_float(data.get('price'))
#规格
spec=data.get('spec')
#原价
market_price=int_to_float(data.get('market_price'))
#详情内容
share_cOntent=data.get('share_content')
#标题
sub_title=data.get('sub_title')
print('---------------------------------------------商品: '+name+'------------------------')
print('规格:'+spec)
print('价格:'+ str(price))
print('原价/折扣:'+str(market_price)+'/'+str(price))
print('详情内容:'+share_content)
print()
print('-----------------------------------------------商品: "'+name+'"的价格波动------------------------')
def t2():
#延时执行
while(1):
t=random.randint(60,300)
print('距离下一次抓取'+str(t)+'秒')
time.sleep(t)
#发送请求
response_1=requests.get(url, headers=headers)
#设置编码
response_1.encoding='utf-8'
#获取内容
c=response_1.text
#转换成字典
dict=json.loads(c)
data=dict.get('data')
#商品价格
price=int_to_float(data.get('price'))
#输出当前价格
print('当前时间为'+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())+', 价格为'+str(price))