python爬虫爬取百度翻译
时间: 2023-11-08 15:51:37 浏览: 193
python爬虫之爬取QQSpider源代码.7z
可以使用Python的第三方库`requests`和`beautifulsoup`来实现爬取百度翻译的功能。下面是一个简单的示例代码:
```python
import requests
from bs4 import BeautifulSoup
def translate(text):
url = 'https://fanyi.baidu.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;Win64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
data = {
'kw': text
}
response = requests.post(url, headers=headers, data=data)
soup = BeautifulSoup(response.text, 'html.parser')
result = soup.find('span', class_='target-output').get_text()
return result
text_to_translate = 'Hello World'
translation = translate(text_to_translate)
print(translation)
```
阅读全文