爬取一本小说全文

import requests
import re
import time

headers={    
    #'User-Agent':'Nokia6600/1.0 (3.42.1) SymbianOS/7.0s Series60/2.0 Profile/MIDP-2.0 Configuration/CLDC-1.0'
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
}
f=open('C:/Users/Administrator/Desktop/lianxi/doupo.txt','a+')

def get_info(url):
    res=requests.get(url,headers=headers)
    #print(res.content.decode('utf-8')        )

    if res.status_code==200:
        contents=re.findall('<p>(.*?)</p>',res.content.decode('utf-8'),re.S)
        for content in contents:
            f.write(content+'\n')
    else:
        print("失败")

if __name__=='__main__':
    urls=['http://www.doupoxs.com/doupocangqiong/{}.html'.format(str(i)) for i in range(6,9)]
    for url in urls:
        get_info(url)
        time.sleep(1)
f.close()

 

posted @ 2018-04-08 15:40  蓝勃斐重新开始  阅读(250)  评论(0编辑  收藏  举报