网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
03月04日漏签0天
python吧 关注:481,644贴子:1,985,750
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 0回复贴,共1页
<<返回python吧
>0< 加载中...

请问一下这个怎么改啊,链接出来了但下载不了

  • 只看楼主
  • 收藏

  • 回复
  • 贴吧用户_G2DQyAb
  • 白丁
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import urllib.request
import urllib.parse
from lxml import html
import chardet
import urllib
import os
import time
from urllib import error
import lxml.html
# 确保 novel_base_url 和 Host 一致
novel_base_url = 'https://www.blqukk.cc'
novel_url = urllib.parse.urljoin(novel_base_url, '38_38836/')
chapter_url_list = []
headers = {
'Host': 'http://www.blqukk.cc',
'Referer': 'https://www.blqukk.cc/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0'
}
def fetch_chapter_urls():
try:
req = urllib.request.Request(url=novel_url, headers=headers)
response = urllib.request.urlopen(req)
content = response.read()
# 自动检测编码
detected_encoding = chardet.detect(content)['encoding']
if detected_encoding:
html_content = content.decode(detected_encoding, errors='ignore')
else:
print("无法检测编码,尝试使用 UTF-8 解码")
html_content = content.decode('utf-8', errors='ignore')
tree = html.fromstring(html_content)
# 修正 XPath 表达式
hrefs = tree.xpath('//dd/a/@href')
for href in hrefs[12:]:
chapter_url_list.append(urllib.parse.urljoin(novel_base_url, href))
print(chapter_url_list)
except Exception as e:
print(f"发生错误: {e}")
if __name__ == '__main__':
fetch_chapter_urls()
nove_save_dir=os.path.join(os.getcwd(),'novel_cache/')
def parsing_chapter(url):
req=urllib.request.Request(url=url,headers=headers)
html=lxml.html.parse(urllib.request.urlopen(req))
title=html.xpath('//h1/text()')[0]
contents=html.xpath('//*[@id="content"]/text()')
content=''
for i in contents:
content+=i.strip()
save_novel(title,content)
def save_novel(name,content):
try:
with open (nove_save_dir+name+'.txt',"w+")as f:
f.write(content.strip())
except(error.HTTPError,OSError) as reason:
print(str(reason))
else:
print("下载完成:"+name)
if __name__ == '__main__':
if not os.path.exists(nove_save_dir):
os.mkdir(nove_save_dir)
fetch_chapter_urls()
for chapter in chapter_url_list:
time.sleep(1)
parsing_chapter(chapter)


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 0回复贴,共1页
<<返回python吧
分享到:
©2026 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示