您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
需要你的python安装有requests模块,如果没有安装可执行如下命令安装
pip3 install requests
以最近比较火的小说“魔道祖师”为例。
下面是整个脚本
import requests,re
def get_content(url,timeout=10):
req = requests.get(url=url,timeout=timeout)
return req.text
def get_title(html,re_title):
ret = re_title.search(html)
if ret:
ret = ret.group()
tmp = ret.split('_')[0]
tmp = tmp.replace('<title>','')
tmp = tmp.strip()
return tmp
def get_body(html,ret_body):
ret_body = re_body.search(html)
if ret_body:
ret = ret_body.group()
tmp = re_clear_header.sub(r'\2',ret)
tmp = tmp.replace(r' ',' ').replace(r'<br /><br />','\n').replace(r'<br />','\n')
tmp = tmp.replace(r'2k小说阅读网</p>','\n\n')
return tmp
if __name__ == '__main__':
mdzs = open('mdzs.txt','w')
re_title = re.compile(r'<title>(.*?)</title>')
re_body = re.compile(r'<p class="Text">(.*?)</p>',re.S)
re_clear_header = re.compile(r'(.*</script>)(.*)',re.S)
first_page = 19613532
for i in range(116):
page = first_page + i
url = r'https://www.2kxs.com/xiaoshuo/96/96717/{}.html'.format(page)
try:
html = get_content(url)
title = get_title(html,re_title)
mdzs.write(title + '\n\n')
body = get_body(html,re_body)
mdzs.write(body)
print('{} is success'.format(url))
except Exception as e:
print('url :{} , error: {}'.format(url,e))该网站是小说网站,排版和网页的url比较有规律性,所以实现起来比较简单
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。