常规动态网页爬取

1.抓取动态网页“http://www.ptpress.com.cn”内容,将新书推荐中生活板块的书籍书名、价格和作者爬取并保存。

import requests
import json
import openpyxl

url = 'https://www.ptpress.com.cn/recommendBook/getRecommendBookListForPortal?bookTagId=d5cbb56d-09ef-41f5-9110-ced741048f5f'
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',
    'Cookie':'gr_user_id=796019e3-dc58-40f5-a6df-892a38008bcd; '
             'acw_tc=2760822416373059896443147efcf3dd457a5539d63a07fdafd12f3041cd93; '
             'JSESSIONID=A0FD72E84771D06417CF145392DAA679; '
             'gr_session_id_9311c428042bb76e=1a1d8cc2-0de9-4409-adc4-07de4cdb503f;'
             ' gr_session_id_9311c428042bb76e_1a1d8cc2-0de9-4409-adc4-07de4cdb503f=true'
}
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 = ['书名', '作者', '价格']
    sheet.append(title)

    for re in res['data']:
        author, discountPrice = json_detail(re['bookId'])
        sheet.append([re['bookName'], author, discountPrice])

    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']
    author = res['author']
    discountPrice = res['discountPrice']
    print(res['bookName'], author, discountPrice)
    return author, discountPrice

save_execl(res)

爬取结果:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马龙强_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值