目录
-
- 一,请求页面,获取bookId
- 二、模拟post请求,获取详细信息
- 三、将数据保存到execl表
- 四、完整代码
一,请求页面,获取bookId
import requests
import json
url = 'https://www.ptpress.com.cn/masterpiece/getMasterpieceListForPortal'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44'
}
text_json = requests.get(url=url,headers=headers)
res = json.loads(text_json.content)
for re in res:
print(re['bookId'])
二、模拟post请求,获取详细信息
def json_detail(bookid):
url = 'https://www.ptpress.com.cn/bookinfo/getBookDetailsById'
bookid = bookid
params = {
'bookId':bookid,
}
text_json = requests.post(url=url, headers=headers,params=params)
res = json.loads(text_json.content)['data']
Book_data = res['bookDetail']['data']
print(res['author'],res['discountPrice'],Book_data['isbn'],Book_data['edition'],Book_data['frame'],Book_data['format'],Book_data['publishDate'],Book_data['pages'])
return res['author'],res['discountPrice'],Book_data['isbn'],Book_data['edition'],Book_data['frame'],Book_data['format'],Book_data['publishDate'],Book_data['pages']
三、将数据保存到execl表
def save_execl(res):
wb1 = openpyxl.Workbook()
sheet = wb1.active
sheet.title = "人民邮电精品力作"
title = ['书名', '书id', '图片地址','作者','价格','ISBN','语种','包装','开本','出版时间','页数']
sheet.append(title)
for re in res['data']:
author,discountPrice,isbn,edition,frame, format,publishDate, pages=json_detail(re['bookId'])
sheet.append([re['bookName'],re['bookId'],re['picPath'],author,discountPrice,isbn,edition, frame, format,publishDate, pages])
wb1.save('数目表.xlsx')
四、完整代码
import requests
import json
import openpyxl
url = 'https://www.ptpress.com.cn/masterpiece/getMasterpieceListForPortal'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44'
}
text_json = requests.get(url=url,headers=headers)
res = json.loads(text_json.content)
def save_execl(res):
wb1 = openpyxl.Workbook()
sheet = wb1.active
sheet.title = "人民邮电精品力作"
title = ['书名', '书id', '图片地址','作者','价格','ISBN','语种','包装','开本','出版时间','页数']
sheet.append(title)
for re in res['data']:
author,discountPrice,isbn,edition,frame, format,publishDate, pages=json_detail(re['bookId'])
sheet.append([re['bookName'],re['bookId'],re['picPath'],author,discountPrice,isbn,edition, frame, format,publishDate, pages])
wb1.save('数目表.xlsx')
def json_detail(bookid):
url = 'https://www.ptpress.com.cn/bookinfo/getBookDetailsById'
bookid = bookid
params = {
'bookId':bookid,
}
text_json = requests.post(url=url, headers=headers,params=params)
res = json.loads(text_json.content)['data']
Book_data = res['bookDetail']['data']
print(res['author'],res['discountPrice'],Book_data['isbn'],Book_data['edition'],Book_data['frame'],Book_data['format'],Book_data['publishDate'],Book_data['pages'])
return res['author'],res['discountPrice'],Book_data['isbn'],Book_data['edition'],Book_data['frame'],Book_data['format'],Book_data['publishDate'],Book_data['pages']
save_execl(res)