吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2237|回复: 3
收起左侧

[Python 原创] python写的小说抓取源码【原创】

[复制链接]
seeyou_shj 发表于 2020-7-27 11:16
闲来无事,不想学C++了。听说最近流行的python很火,研究一下。

顺手将小说抓取完善一下。源码列在下面了。需要的自行整理吧。很简单,也就不做说明了。

觉得有用的,给个免费的热心值支持一下吧。

from urllib.request import urlopen

myurl = 'https://www.zwdu.com/book/31855/'
myhost = myurl[0:myurl.find("/",10)]
f = open('d:/text.txt','w+',encoding='gbk')
smsg = urlopen(myurl).read().decode('gbk')
tmsg = smsg.find("<dd>")
while tmsg > 0:
    t = smsg[tmsg:smsg.find("</dd>",tmsg)]
    smsg = smsg[smsg.find("</dd>",tmsg):]
    tmsg = smsg.find("<dd>")
    chapurl = myhost + t[t.find("\"") + 1 : t.find("\"",t.find("\"") + 2)]
    chapname = t[t.find("\">")+2:t.find("</a>",t.find("\">")+6)]+"\n"
    temp = urlopen(chapurl).read().decode('gbk')
    content = temp[temp.find("<div id=\"content\">")+18:temp.find("</div>",temp.find("<div id=\"content\">")+20)] + "\n"
    content = content.replace("\t","")
    content = content.replace("<br />","\n")
    f.write(chapname)
    f.write(content)
f.close()

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
不是童画 + 1 + 1 https://www.52pojie.cn/thread-1437556-1-1.html帮我看看这个。谢谢

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

黑了黑 发表于 2020-7-27 13:25
谢谢分享,学习
 楼主| seeyou_shj 发表于 2020-8-19 13:29
遇到解码问题,搜索后找到解决方法。源码经过修改如下:
import re
from urllib.request import urlopen

myurl = 'http://www.purepen.com/hlm/'
myhost = myurl
f = open('d:/mytemp/红楼梦.txt','w+',encoding='gb18030')
smsg = urlopen(myurl).read()
#选择解码字符集
if re.search(b'[a-zA-Z0-9\-]*',smsg[smsg.find(b'charset=')+8:]).group() == b'GB2312' \
   or re.search(b'[a-zA-Z0-9\-]*',smsg[smsg.find(b'charset=')+8:]).group() == b'gb2312':
    charset = 'gb18030'
if re.search(b'[a-zA-Z0-9\-]*',smsg[smsg.find(b'charset=')+8:]).group() == b'GBK' \
   or re.search(b'[a-zA-Z0-9\-]*',smsg[smsg.find(b'charset=')+8:]).group() == b'gbk':
    charset = 'gbk'
if re.search(b'[a-zA-Z0-9\-]*',smsg[smsg.find(b'charset=')+8:]).group() == b'UTF-8' \
   or re.search(b'[a-zA-Z0-9\-]*',smsg[smsg.find(b'charset=')+8:]).group() == b'utf-8':
    charset = 'utf-8'
smsg = smsg.decode(charset)   #解码
tmsg = smsg.find("<TD>第 一 回")
t = smsg[tmsg:smsg.find("</TABLE>",tmsg)]
tmsg = t.find("<A HREF")
while tmsg > 0:
    #smsg = smsg[smsg.find("<a",tmsg):]
    #tmsg = smsg.find("</a>")
    chapurl = myhost + t[tmsg + 9 : t.find("\"",tmsg + 12)]
    #chapname = t[t.find("html\">")+6:t.find("</a>",t.find("html\">")+6)]+"\n"
    tmsg = t.find("</A>")
    t = t[tmsg+6:]
    tmsg = t.find("<A HREF")
    temp = urlopen(chapurl).read().decode('gb18030')
    chapname = temp[temp.find("<b>")+3:temp.find("</b>")] + '\n'
    f.write(chapname)
    temp = temp[temp.find("<table>"):temp.find("</table>")]
    content = temp[temp.find("size=\"3\">")+9:temp.find("</font>")] + "\n"
    content = content.replace("\t","")
    content = content.replace("<br />","")
    content = content.replace("&nbsp;","")
    temp = re.split(r'\n',content)
    for i in range(len(temp)):
        if len(temp[i])<33:
            temp[i] = temp[i]+'\n'
        f.write(temp[i])
f.close()
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 04:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表