要用Python爬取文本内容并保存,可以按照以下步骤进行:
requests
库,用于发送HTTP请求获取网页内容;导入BeautifulSoup
库,用于解析网页内容。import requests
from bs4 import BeautifulSoup
requests
库的get
方法发送GET请求,并通过text
属性获取网页内容。url = '要爬取的网页URL'
response = requests.get(url)
html = response.text
BeautifulSoup
库解析网页内容,并提取所需的文本信息。soup = BeautifulSoup(html, 'html.parser')
text = soup.get_text()
open
函数打开一个文件,然后使用write
方法写入内容。with open('保存的文件路径', 'w', encoding='utf-8') as file:
file.write(text)
完整代码示例:
import requests
from bs4 import BeautifulSoup
url = '要爬取的网页URL'
response = requests.get(url)
html = response.text
soup = BeautifulSoup(html, 'html.parser')
text = soup.get_text()
with open('保存的文件路径', 'w', encoding='utf-8') as file:
file.write(text)
请将代码中的要爬取的网页URL
替换为你需要爬取的网页的URL,保存的文件路径
替换为你希望保存的文件路径。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: python中怎么提取指定元素