Python爬虫利器之Beautiful Soup的用法

BeautifulSoup的帮助说明可以参考 http://cuiqingcai.com/1319.html


爬取网络小说

#-*- coding:utf-8 -*-

import urllib.request
import re
from bs4 import BeautifulSoup
import os

def getPage(url):
    page = urllib.request.urlopen(url).read().decode('utf-8')
    return page

def saveToFile(page,num):
    soup = BeautifulSoup(page)
    contents = soup.p.prettify()
    print(contents)
    contents = contents.replace("<br/>","").replace("/bk","").replace('<p>',"").replace('</p>',"")
    print("=======================")
    print(contents)
   
    title = '第'+str(num)+'章.txt'
    print(title)
    with open(title,'w') as file:
        file.writelines(contents)

if __name__=='__main__':
    if os.path.exists('死人经'):
        os.removedirs('死人经')
    os.mkdir("死人经")
    os.chdir("死人经")
    j = 98
    for i in range(620,653):
        url = "http://h6w.org.cn/post/" + str(i) + ".html"
        print(url)
        j = j+1
        saveToFile(getPage(url),j)


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值