爬取笔趣网

import requests
from bs4 import BeautifulSoup
import html
import re
import time
def get_html_txt(url):
    try:
        html=Sess.get(url=url)
        html.encoding=html.apparent_encoding
        txts=BeautifulSoup(html.text,'html.parser')
        return txts.select('#content')[0].get_text()
    except:
        get_html_txt(url)
def wt(txt,tit):
    txt=str(txt)
    tit=str(tit).replace('*','')
    tit = str(tit).replace('~', '')
    tit = str(tit).replace('/', '')
    tit = str(tit).replace('(', '')
    tit = str(tit).replace(')', '')
    tit = str(tit).replace('【', '')
    tit = str(tit).replace('】', '')
    with open('./超神机械师/'+tit+'.txt','w',encoding='utf-8') as f:
        f.write(txt)
if __name__ == '__main__':
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',  # 这里一定要改成no-cache否则会缓存304二次访问调试不会反回内容
        'Connection': 'keep-alive',
        'Cookie': 'clickbids=40160; Hm_lvt_c979821d0eeb958aa7201d31a6991f34=1584758773; Hm_lvt_6dfe3c8f195b43b8e667a2a2e5936122=1584758818; Hm_lpvt_6dfe3c8f195b43b8e667a2a2e5936122=1584758818; Hm_lpvt_c979821d0eeb958aa7201d31a6991f34=1584758818',
        'Host': 'www.biquge.info',
        # 'If-Modified-Since': 'Thu, 19 Dec 2019 16:37:49 GMT', #禁止缓存
        # 'If-None-Match': 'W/"5dfba75d-5b43"', #禁止缓存,参考https://blog.csdn.net/qq_37960324/article/details/83374855
        'Referer': 'http://www.biquge.info/40_40160/',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
    }
    Sess = requests.session()
    Sess.headers = headers
    url = 'http://www.biquge.info/40_40160/'
    html_txt = Sess.get(url=url)
    html_txt.encoding = html_txt.apparent_encoding
    txts = BeautifulSoup(html_txt.text, 'html.parser')
    urls=[]
    for t in txts.select('#list > dl > dd'):
        href=t.a['href']
        title=t.a['title']
        urls.append((url+href,title))
    for u in urls:
        #print(u)
        txt=get_html_txt(u[0])
        wt(txt,u[1])
        time.sleep(3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值