逆天邪神吧 关注:781,032贴子:11,161,567
  • 5回复贴,共1

用Python写一个爬虫爬取《逆天邪神》

只看楼主收藏回复

仅供学习交流使用
import time as ti
import requests
from bs4 import BeautifulSoup
# 设置封面URL
book_url = 'https://www.xbiquge.bz/book/7138/'
response = requests.get(book_url)
soup = BeautifulSoup(response.content, 'lxml')
# 提取dl tag, 这里面包含了dd tag, 是章节的入口
soup = soup.find('dl')
# 提取dd tag 25去掉则下载全本
a_list = soup.find_all('a')[12:25]
t1 = ti.time()
# 打开文件,参数1是文件路径,参数2是打开模式('w'表示写入模式)
with open('逆天邪神.txt', 'w', encoding='utf-8') as file:
for a in a_list:
href = a.get('href')
chap_url = book_url + href
response = requests.get(chap_url)
soup = BeautifulSoup(response.content, 'lxml')
# 提取章节标题
title = soup.title.get_text()
# 提取正文
maintext = soup.find('div', {'id':'content'}).get_text()
maintext = maintext.replace('$ ()', '')
maintext = maintext.replace('\xa0\xa0', '\n')
maintext = title + '\n' + maintext + '\n\n\n'
# 写入内容
file.write(maintext)
t2 = ti.time()
print('用时:', t2 - t1, 's')


IP属地:中国香港1楼2023-11-11 19:10回复


    IP属地:河南来自Android客户端2楼2023-11-11 20:35
    回复
      牛逼。。。


      IP属地:广东来自Android客户端3楼2023-11-11 21:06
      回复
        能扒VIP不?


        IP属地:甘肃4楼2023-11-11 21:12
        回复
          笔趣阁啊?那没啥用,最新章节当天又看不了。


          IP属地:甘肃5楼2023-11-11 21:13
          回复
            有没有黑客能把🔥🐶的电脑给黑了,这🐶肯定早就写完了


            IP属地:湖南来自iPhone客户端6楼2023-11-11 22:01
            回复