爬取人民邮电出版社书籍信息

目录

    • 一,请求页面,获取bookId
    • 二、模拟post请求,获取详细信息
    • 三、将数据保存到execl表
    • 四、完整代码

一,请求页面,获取bookId

爬取人民邮电出版社书籍信息_第1张图片

import requests
import json


# 获取bookId
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张图片
爬取人民邮电出版社书籍信息_第3张图片

def json_detail(bookid):
	# post请求,需要传递参数,参数就是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'])
    # ['书名', '书id', '图片地址','价格','作者','ISBN','语种','包装','开本','出版时间','页数']
    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张图片

四、完整代码

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/getBookListForRecommend'
    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'])
    # ['书名', '书id', '图片地址','价格','作者','ISBN','语种','包装','开本','出版时间','页数']
    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)

你可能感兴趣的:(爬虫,python,开发语言,后端)