【python爬虫小实战】python3.x用requests和bs4实现有道翻译(中英文)
2017-08-07 00:00
931 查看
一直用的是python3.x版本的,刚开始学爬虫的时候学长给了我个爬有道翻译的小程序,实现中英文翻译,由于是用urllib库的,当时也是刚接触python,所以一脸懵逼,现在学了一个月了,回头再看了一下,感觉很时间单,于是就用requests库和bs4,加上json网页解析,也写 了个翻译小程序,(感觉比用urllib代码要少的多,)其实开始requests.get的方法参数很懵逼,百度了一下,总算把这点弄明白。越发感觉python库的强大
代码很简单,很容易就能明白的,欢迎大佬们测试(#滑稽)
#python 爬虫有道翻译 import requests from bs4 import BeautifulSoup import json while True: content = input('请输入要查询的单词/词语(输入0退出翻译):') if content == '0': print('欢迎下次使用!') break url = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&sessionFrom=null' headers = { 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36' ,'from':'AUTO', 'to':'AUTO', 'smartresult':'dict', 'client':'fanyideskweb', 'doctype':'json', 'version':'2.1', 'keyfrom':'fanyi.web', 'action':'FY_BY_ENTER', 'typoResult':'true', 'i':content ,'ue':'UTF-8'#设置翻译支持中文 } res = requests.get(url,params = headers) soup = BeautifulSoup(res.text,'lxml') jd = json.loads(soup.text) print('翻译结果:') for translate in jd['smartResult']['entries']: print(translate) print('\n')
代码很简单,很容易就能明白的,欢迎大佬们测试(#滑稽)
相关文章推荐
- Python3下基于bs4和sqlalchemy的爬虫实现
- requests和bs4的python爬虫入门
- 【Python实战】机型自动化标注(搜狗爬虫实现)
- python3[爬虫实战] 爬虫之requests爬取新浪微博京东客服
- Python爬虫实战三之实现山东大学无线网络掉线自动重连
- 简单的python2.7基于bs4和requests的爬虫
- Python 爬虫实战(一):使用 requests 和 BeautifulSoup
- Python - 通过requests实现腾讯新闻抓取爬虫
- python爬虫基础知识——requests、bs4的使用
- Python爬虫实例——基于BeautifulSoup和requests实现
- python3 [爬虫实战] selenium + requests 爬取安居客
- python3 [爬虫实战] selenium + requests 爬取安居客
- 【Python爬虫实战--1】深入理解urllib;urllib2;requests
- Python爬虫实现简单的爬取有道翻译功能示例
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- [Python 实战] - No.2 Python实现微博爬虫
- Python 爬虫实战(二):使用 requests-html
- 超具实战意义的Python项目课程:四周实现爬虫系统 超经典的Python零基础实战化教程
- Python Beautiful Soup+requests实现爬虫
- 爬虫学习3.2 HTTP请求的python实现--Requests