使用Python爬虫抓取网页文本可以使用第三方库requests和beautifulsoup。
首先,安装requests和beautifulsoup库:
pip install requestspip install beautifulsoup4然后,编写Python代码:
import requestsfrom bs4 import BeautifulSoup# 发送GET请求,获取网页内容url = "http://example.com" # 要抓取的网页URLresponse = requests.get(url)html = response.text# 使用BeautifulSoup解析网页内容soup = BeautifulSoup(html, 'html.parser')# 提取网页文本text = soup.get_text()print(text)以上代码中,首先使用requests库发送GET请求,获取网页内容并保存在变量html中。然后,使用BeautifulSoup库将网页内容进行解析,生成一个BeautifulSoup对象soup。最后,使用soup的get_text()方法提取网页的纯文本内容,并打印出来。
需要注意的是,使用爬虫抓取网页文本时,需要遵守网站的使用规则,尊重网站的robots.txt文件,不进行恶意爬取。另外,可能需要处理一些反爬机制,如设置User-Agent、延时请求等。

