使用python的bs4库爬取笔趣阁的一篇小说

import requests
import re
import os
from bs4 import BeautifulSoup
catalog_list_name=[]
catalog_list_href=[]
catalog_list_url=[]

def HTMLToSoup(url):
	html=requests.get(url,headers={'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95'})
	html.encoding=html.apparent_encoding
	soup=BeautifulSoup(html.text,'html.parser')
	return soup

url='http://www.xbiquge.la/15/15003/'
soup=HTMLToSoup(url)
catalog_html=soup.find(name='body').find(name='div',id='list').find('dl').find_all('dd')
for i in catalog_html:
	catalog_list_name.append(i.find('a').string)
	catalog_list_href.append(i.find('a').attrs['href'])
for i in catalog_list_href:
	catalog_list_url.append('http://www.xbiquge.la'+i)

def get_content(url):
	content_soup=HTMLToSoup(url)
	text=content_soup.find(name='div',id='content')
	return text.text
def put_all_content(mingzilist,urllist2):
	s=1
	if os.path.exists('C:\\Users\\Administrator\\Desktop\\xiaoshuo'):
		print('目录已经存在')
	else:
		os.mkdir('C:\\Users\\Administrator\\Desktop\\xiaoshuo')
		print('目录不存在,创建')
	for u in urllist2:
		with open('C:\\Users\\Administrator\\Desktop\\xiaoshuo\\'+mingzilist[s]+'.txt','w',encoding='utf-8') as f:
			f.write(get_content(u))
		print('已爬取',s,'章')
		s=s+1
		
put_all_content(catalog_list_name,catalog_list_url)
	
	

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值