Python——获取网页文本内容

01 实现背景

1、免费小说网站:http://book.zongheng.com/,我们获取的文字信息就来源于该网站

2、requests模块,用于http形式请求访问网页

3、BeautifulSoup模块,用于解析获取到的网页内容



02 实现目标

首先利用requests模块获取网页源码,通过BeautifulSoup模块进一步筛选获得文本内容


03 注意事项

如需将获取内容输入到本地文件,可自行利用with…open操作



04 实现代码

import requests 
from bs4 import BeautifulSoup

url = 'http://book.zongheng.com/chapter/897468/58575172.html'
resp = requests.get(url = url)
html = resp.text
soup = BeautifulSoup(html,"html.parser")
try:
	for i in  range(50):
		print(soup.find_all('p')[i].string)
except:
	pass



05 实现效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq2539879928/article/details/106176843