使用python百度翻译爬虫
时间: 2023-12-17 07:27:44 浏览: 121
python 爬虫如何实现百度翻译
以下是使用Python实现百度翻译爬虫的步骤:
1.导入requests和BeautifulSoup库
```python
import requests
from bs4 import BeautifulSoup
```
2.设置请求头和请求参数
```python
url = 'https://fanyi.baidu.com/sug'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
data = {
'kw': 'hello'
}
```
3.发送POST请求并获取响应
```python
response = requests.post(url, headers=headers, data=data)
```
4.解析响应内容并输出翻译结果
```python
soup = BeautifulSoup(response.text, 'html.parser')
result = soup.find_all('span', class_='keyword')
for r in result:
print(r.get_text())
```
阅读全文