python爬取外网文本
时间: 2023-06-29 14:18:54 浏览: 101
要爬取外网文本,可以使用 Python 的 requests 库来发起 HTTP 请求,获取网页内容,然后使用 BeautifulSoup 或者正则表达式等方式来解析网页内容,获取文本。
以下是一个简单的示例代码,可以用来爬取某个网页的文本内容:
```python
import requests
from bs4 import BeautifulSoup
# 指定要爬取的网页 URL
url = "https://www.example.com"
# 发起 HTTP 请求,获取网页内容
response = requests.get(url)
# 使用 BeautifulSoup 解析网页内容
soup = BeautifulSoup(response.text, "html.parser")
# 获取文本内容
text = soup.get_text()
# 打印文本内容
print(text)
```
需要注意的是,爬取外网文本需要遵守网站的爬虫协议,不要频繁访问网站,也不要过度爬取网站内容。此外,一些网站可能会对爬虫行为进行检测和限制,请务必尊重网站规定。
相关问题
python爬取中文网站文本
要爬取中文网站的文本,你可以使用Python中的第三方库BeautifulSoup和Requests来实现。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求获取网页内容
url = 'http://example.com' # 替换为你要爬取的网站的URL
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html, 'html.parser')
# 提取中文文本
chinese_text = ''
for text in soup.find_all(text=True):
if text.strip() != '':
chinese_text += text.strip()
print(chinese_text)
```
这段代码首先使用`requests`库发送请求获取网页内容,然后使用`BeautifulSoup`库解析网页内容。最后,通过遍历`find_all(text=True)`方法返回的文本节点,提取其中的中文文本。
python爬取央视新闻文本
要爬取央视新闻文本,可以使用Python中的requests和BeautifulSoup库。
首先,使用requests库发送HTTP请求获取央视新闻网站的HTML源代码:
```python
import requests
url = "http://news.cctv.com/"
response = requests.get(url)
html = response.content
```
然后,使用BeautifulSoup库解析HTML源代码,提取新闻标题和内容:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, "html.parser")
news_list = soup.find_all("div", class_="newslist")
for news in news_list:
title = news.find("a").text
link = news.find("a")["href"]
response = requests.get(link)
soup = BeautifulSoup(response.content, "html.parser")
content = soup.find("div", class_="cnt_bd").text
print(title)
print(content)
```
这样就可以爬取央视新闻网站上的新闻文本了。注意,为了避免被网站屏蔽,最好加上一些延时和随机User-Agent等防止反爬措施。