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 實現效果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章