Python爬取三亚学院社会学院所有p标签文本并保存到桌面
import requests from bs4 import BeautifulSoup import os
设置保存路径为桌面
desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
请求页面
url = 'http://soc.sya.edu.cn/' res = requests.get(url)
解析页面
soup = BeautifulSoup(res.text, 'html.parser')
获取所有p标签中的文本
p_list = soup.find_all('p') text_list = [p.text for p in p_list]
将文本保存到桌面
with open(desktop_path + '/text.txt', 'w', encoding='utf-8') as f: for text in text_list: f.write(text + ' ') print('保存成功!')
原文地址: https://www.cveoy.top/t/topic/nhQr 著作权归作者所有。请勿转载和采集!