python 【面向对象】

 1 #百度翻译
 2 
 3 import json
 4 import requests
 5 
 6 
 7 class Spider:
 8     def __init__(self):
 9 
10         self.session = requests.Session()
11         self.rq = requests.request
12 
13     def run(self,word):
14         self.search(word)
15 
16      #查找word
17     def  search(self,word):
18         url = "http://fanyi.baidu.com/sug"
19         data ={
20 
21               'kw':word
22 
23             }
24         headers ={
25           "User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36",
26             }
27  
28       
29             
30         #发送请求
31         response = self.session.post(url,headers=headers,data=data)
32         #字符串转换为json
33         info = json.loads(response.text)
34         print(info)
35         
36 
37 if __name__ == "__main__":
38 
39     spider = Spider()
40     spider.run('world')
41     

 

posted @ 2018-08-06 10:57  Justice-V  阅读(78)  评论(0)    收藏  举报