目录
-
- 一,请求页面,获取bookId
- 二、模拟post请求,获取详细信息
- 三、将数据保存到execl表
- 四、完整代码
一,请求页面,获取bookId
![爬取人民邮电出版社书籍信息_第1张图片](http://img.e-com-net.com/image/info8/8afd0e94a220484a82617a25496d27af.jpg)
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请求,获取详细信息
![爬取人民邮电出版社书籍信息_第2张图片](http://img.e-com-net.com/image/info8/1027bc2bab354fe79d28a77f57bde71f.jpg)
![爬取人民邮电出版社书籍信息_第3张图片](http://img.e-com-net.com/image/info8/307e5a34975a4b92955b19e8a1884223.jpg)
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')
![爬取人民邮电出版社书籍信息_第4张图片](http://img.e-com-net.com/image/info8/391d1311172d42119f0c277605c22e5e.png)
四、完整代码
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)