yanwii 7 years ago
parent
commit
bb69e05210
5 changed files with 14 additions and 5 deletions
  1. 3 0
      .vscode/settings.json
  2. 2 2
      README.md
  3. BIN
      __pycache__/xiaoiceapi.cpython-36.pyc
  4. 3 0
      test.py
  5. 6 3
      xiaoiceapi.py

+ 3 - 0
.vscode/settings.json

@@ -0,0 +1,3 @@
+{
+    "python.linting.pylintEnabled": false
+}

+ 2 - 2
README.md

@@ -17,7 +17,7 @@
 
 
     api模式:
-    py xiaoiceapi.py
+    python3 xiaoiceapi.py
     然后在浏览器中输入
     http://127.0.0.1:5000/?que=你是谁
     
@@ -30,4 +30,4 @@
     "text": "你都知道你还问[不屑脸],哈哈党"  
     }  
 
-    
+    

BIN
__pycache__/xiaoiceapi.cpython-36.pyc


+ 3 - 0
test.py

@@ -0,0 +1,3 @@
+import xiaoiceapi 
+xb = xiaoiceapi.xiaoiceApi()
+print(xb.chat("你是谁"))

+ 6 - 3
xiaoiceapi.py

@@ -20,7 +20,8 @@ class xiaoiceApi():
             line = headers.readline().strip()
             while line:
                 key = line.split(":")[0]
-                self.headers[key] = line[len(key)+1:]
+                #firefox里的原始头冒号后面会多出一个空格,需除去
+                self.headers[key] = line[len(key)+1:].strip()
                 line = headers.readline().strip()            
 
     def chat(self, input_strs):
@@ -48,7 +49,8 @@ class xiaoiceApi():
         }
         
         try:
-            url = 'http://weibo.com/aj/message/add?ajwvr=6'
+            #原http的api已改为https的api
+            url = 'https://weibo.com/aj/message/add?ajwvr=6'
             page = requests.post(url, data=data, headers=self.headers)
             self.savePage(page.text, "./tmp/postpage.txt")
             if page.json()['code'] == '100000':
@@ -72,7 +74,8 @@ class xiaoiceApi():
         times = 1
         while times:
             times += 1
-            response = requests.get("http://weibo.com/aj/message/getbyid?ajwvr=6&uid=5175429989&count=1&_t=0" , headers=self.headers)
+            #同上,原http的api已改为https的api,另外headers用全反而无法获取页面,只需用到cookies
+            response = requests.get("https://weibo.com/aj/message/getbyid?ajwvr=6&uid=5175429989&count=1&_t=0" , headers={"Cookie":self.headers["Cookie"]})
             self.savePage(response.text, "./tmp/response.txt")
             soup = BeautifulSoup(response.json()['data']['html'], "lxml")
             text = soup.find("p", class_='page').text