爬取起点小说免费专区

from turtle import title
import requests
from lxml import etree
import json

url = "https://www.qidian.com/chapter/1038058470/767511431/"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0",
    "cookie": "supportwebp=true; supportWebp=true; _csrfToken=df5a5e7b-feb3-4658-b9c7-58a2a155fdf2; newstatisticUUID=1709133168_1484421828; fu=380175539; Hm_lvt_f00f67093ce2f38f215010b699629083=1723890564; HMACCOUNT=03CDEC9A006B218E; _gid=GA1.2.227453649.1723890564; e2=; e1=%7B%22l6%22%3A%22%22%2C%22l7%22%3A%22%22%2C%22l1%22%3A3%2C%22l3%22%3A%22%22%2C%22pid%22%3A%22qd_p_qidian%22%2C%22eid%22%3A%22qd_A18%22%7D; traffic_search_engine=; se_ref=; traffic_utm_referer=https%3A%2F%2Fcn.bing.com%2F; Hm_lpvt_f00f67093ce2f38f215010b699629083=1723890756; _ga=GA1.1.2081541013.1688432247; _ga_FZMMH98S83=GS1.1.1723890563.6.1.1723890756.0.0.0; _ga_PFYW0QLV3P=GS1.1.1723890563.6.1.1723890756.0.0.0; w_tsfp=ltvuV0MF2utBvS0Q66nhnUquFDsucDo4h0wpEaR0f5thQLErU5mH0o5zv8v1M3za4cxnvd7DsZoyJTLYCJI3dwMUTMjFe9wZjgXDkIAi2YgQBhljR8qOCgRJJ+4k5GNDKHhCNxS00jA8eIUd379yilkMsyN1zap3TO14fstJ019E6KDQmI5uDW3HlFWQRzaLbjcMcuqPr6g18L5a5Tba7QiqeF0iALMR1EbB0SFNDHoj4Ee+d+8OMhukcp+sSqA=",
}
response = requests.get(url, headers=headers)

content = response.text

html = etree.HTML(content, etree.HTMLParser())

json_data = html.xpath("//script[@id='vite-plugin-ssr_pageContext']/text()")[0]
# 转换json数据变成python字典对象
data = json.loads(json_data)

# pageContext-->pageProps-->pageData-->chapterInfo-->content层级结构
chapterName = data["pageContext"]["pageProps"]["pageData"]["chapterInfo"]["chapterName"]
content = data["pageContext"]["pageProps"]["pageData"]["chapterInfo"]["content"]

# 去除<p>标签,变成换行符
content = content.replace("<p>", "\n")
print(content)

以下是使用Python编写的爬取起点小说网的多线程爬虫代码: ```python import requests from lxml import etree from queue import Queue import threading # 定义爬虫类 class Spider(): def __init__(self, url, headers): self.url = url self.headers = headers self.session = requests.Session() self.session.headers.update(headers) # 获取小说列表 def get_novel_list(self): response = self.session.get(self.url) html = etree.HTML(response.text) novel_list = html.xpath('//div[@class="book-mid-info"]/h4/a/@href') return novel_list # 获取小说信息 def get_novel_info(self, novel_url): response = self.session.get(novel_url) html = etree.HTML(response.text) novel_info = {} novel_info['title'] = html.xpath('//div[@class="book-info "]/div[@class="book-info "]/h1/em/text()')[0] novel_info['author'] = html.xpath('//div[@class="book-info "]/div[@class="book-info "]/h1/span/a/text()')[0] novel_info['intro'] = html.xpath('//div[@class="book-intro"]/p/text()')[0] novel_info['word_count'] = html.xpath('//div[@class="book-info "]/div[@class="book-info "]/p/span[1]/text()')[0] return novel_info # 定义爬取线程类 class SpiderThread(threading.Thread): def __init__(self, spider, novel_queue): threading.Thread.__init__(self) self.spider = spider self.novel_queue = novel_queue def run(self): while True: try: novel_url = self.novel_queue.get(False) novel_info = self.spider.get_novel_info(novel_url) print(novel_info) except: break # 定义主函数 def main(): url = 'https://www.qidian.com/all' headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} spider = Spider(url, headers) novel_list = spider.get_novel_list() # 创建小说队列 novel_queue = Queue() # 将小说列表加入队列 for novel_url in novel_list: novel_queue.put(novel_url) # 创建爬取线程 threads = [] for i in range(5): spider_thread = SpiderThread(spider, novel_queue) spider_thread.start() threads.append(spider_thread) # 等待所有线程结束 for t in threads: t.join() if __name__ == '__main__': main() ``` 该代码使用了Python的requests库和lxml库来进行网页爬取和解析,使用了多线程来提高爬取效率。首先定义了一个Spider类来实现爬取小说列表和小说信息的功能,然后定义了一个SpiderThread类来作为爬取线程,最后在主函数中创建小说队列和爬取线程,并等待所有线程结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

0zxm

祝大家天天开心

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

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

打赏作者

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

抵扣说明:

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

余额充值