爬虫学习-简单入门

版权声明:版权所有,转载请说明出处。 https://blog.csdn.net/matlab001/article/details/84022452

今天开始学习爬虫相关知识,学习的主要目的并不是去做信息的收集,主要还是通过这个大众化的知识板块来学习python相关知识。

第一个例子是进行python实现程序角度的自动翻译实例,相关代码如下:

#首先导入需要的包
import  urllib.request as req
import  urllib.parse as pa
import  json as js

#通过用户输入的内容进行翻译
content = input('请输入要翻译的内容:')
#生成data 字典信息
data = {}
data['i'] = content
data['doctype'] = 'json'
data['keyfrom'] = 'fanyi.web'
data = pa.urlencode(data).encode('utf-8')
#打开相关网址
response = req.urlopen("http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule",data)
#读取信息并且进行解码操作
html = response.read().decode('utf-8')
#把读取到的信息通过json解析
tar = js.loads(html)
#打印输出
print('翻译后的结果为:%s' %tar['translateResult'][0][0]['tgt'])

猜你喜欢

转载自blog.csdn.net/matlab001/article/details/84022452
今日推荐