需要你的python安裝有requests模塊,如果沒(méi)有安裝可執(zhí)行如下命令安裝

創(chuàng)新互聯(lián)是專業(yè)的墨脫網(wǎng)站建設(shè)公司,墨脫接單;提供網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行墨脫網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
pip3 install requests
以最近比較火的小說(shuō)“魔道祖師”為例。
下面是整個(gè)腳本
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小說(shuō)閱讀網(wǎng)</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))該網(wǎng)站是小說(shuō)網(wǎng)站,排版和網(wǎng)頁(yè)的url比較有規(guī)律性,所以實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單
網(wǎng)站欄目:用python“爬”一篇小說(shuō)
標(biāo)題路徑:http://www.chinadenli.net/article16/pescdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、做網(wǎng)站、微信小程序、網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)