【Python——爬取纵横小说网指定数据并存入MySql数据库】(修改后)

import json
import re
import chardet
#import requests
import pymysql

img_li = []
title_li = []
author_li = []

for p in range(1, 10):
    url = f'https://book.zongheng.com/store/c0/c0/b0/u4/p1/v0/s9/t0/u0/i1/ALL.html'
    response = requests.get(url=url)
    content = response.content.decode()
    # print(content)
    imgs = re.findall('<img src="(https://static.zongheng.com/upload/cover/.*?)" alt', content)
    titles = re.findall('<a href="https://book.zongheng.com/book/\d+.html" target="_blank">(.*?)</a>', content)
    authors = re.findall('<a href="https://home.zongheng.com/show/userInfo/\d+.html" target="_blank">(.*?)</a>',
                         content)
    # print(imgs)
    book_url = set(re.findall('<a href="https://book.zongheng.com/book/(.*?)" target="_blank">', content))
    url_chapter = 'https://bookapi.zongheng.com/api/chapter/getChapterList'
    headers = {'Cookie': '*******'}
    bookid = re.findall('<a href="https://book.zongheng.com/book/(.*?).html" target="_blank">[\u4e00-\u9fa5]+</a>', content)
    print(bookid, len(bookid))
    data = {'bookId': bookid}
    response_b = requests.post(url=url_chapter, data=data)
    content_b = response_b.content.decode('utf8')
    # print(content_b)
    chapters = json.loads(content_b)
    # print(chapters)
    results = chapters['result']['chapterList'][0]['chapterViewList']
    # print(results)
    li_chapterId = []
    li_chapterName = []
    for result in results:
        li_chapterId.append(result['chapterId'])
        li_chapterName.append(result['chapterName'])

# 存入数据库
con = pymysql.connect(
    host="127.0.0.1",
    user="root",
    password="******",
    port=3306,
    database="db_******"
)
con.autocommit(True)
cursor = con.cursor()

insert_query = ("INSERT INTO book (title, author, img) VALUES (%s, %s, %s)")

# 插入多条数据
for title, author, img in zip(title_li, author_li, img_li):
    cursor.execute(insert_query, (title, author, img))

cursor.close()
con.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值