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()