王龙腾---第二次作业
| 这个作业属于哪个课程 | 至诚软工实践F班 |
|---|---|
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/fzzcxy/ZhichengSoftengineeringPracticeFclass/homework/12532 |
| 这个作业的目标 | <完成第二次作业,学习爬虫> |
| Github 地址 | https://github.com/teotfw0012/pupu/tree/master |
第二次作业
解体思路
刚开始拿到这次作业时,发现都是自己没学过,但是都有听说过的,所以就是直接在b站找教程,先从爬虫开始学,不过后来有一些同学的帮助和一些网上的资料,开始写这次作业。
fiddler学习+爬虫
修改fiddler的配置文件,由于跟学校校园网冲突,所以拿手机开热点爬虫,登入客户端的微信小程序,开始爬虫,拿到最重要的两个数据
获得到的url地址,在火狐打开,就能得到比较清晰的数据。
代码实现
import json
from time import strftime, sleep
import requests
# 导入需要的包
def request_url():
url = 'https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/db1fabfb-0892-4110-95f6-0f55f294358f' #json文件
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36' #请求头
}
res = requests.get(url, headers=head)
dictionary = json.loads(res.text)
name = dictionary["data"]["name"] #名字
gg = dictionary["data"]["spec"] #规格
price = str(int(dictionary["data"]["price"]) / 100) #价钱
marketPrice = str(int(dictionary["data"]["market_price"]) / 100)
content = dictionary["data"]["share_content"]
print("-------------商品:" + name + "-------------")
print("规格:" + gg)
print("原价:" + price)
print("原价/折扣价:" + price + "/" + marketPrice)
print("详细内容:" + content)
def time(): #实时监控价钱
url = 'https://j1.pupuapi.com/client/product/storeproduct/detail/deef1dd8-65ee-46bc-9e18-8cf1478a67e9/db1fabfb-0892-4110-95f6-0f55f294358f'
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
}
res = requests.get(url, headers=head)
dictionary = json.loads(res.text)
name = dictionary["data"]["name"]
price = str(int(dictionary["data"]["price"]) / 100)
print("-------------" + name + "-------------")
try:
while (True):
currentTime = strftime('%Y' + '-' + '%m' + '-' + '%d' + ' %H:%M:%S,价格为' + price)
print(currentTime)
sleep(5)
except:
print("程序结束")
if __name__ == '__main__': #主程序
request_url()
print("\n")
time()
上传git
我是直接在idea上跟自己的GitHub账号连接,根据网上的一些资料,不需要命令行提交项目,直接在idea上本地建立仓库提交项目
问题与解决
由于使用fiddler的时候,与校园网冲突,所以需要连自己手机热点,但是我在爬完数据后,要运行代码时,退出热点,电脑上右下角的图标还是显示有网,我以为校园网自动重连了,程序运行时,没有任何报错与输出,就觉得奇怪,后面研究了很长时间,以为用热点爬的数据,要用热点才能继续运行,后来才知道,是我当时退热点的时候,校园网没有自动重连,图标显示有bug,在爬数据时,没有连网,是无法输出任何数据的。
还有谢谢那些同学帮助,还有一些陌生的开源程序员的无私奉献精神,让我完成了这次作业。