ソースを参照

Merge pull request #6 from QingGo/patch-1

除去firefox的原始头读取bug,更新api访问
yanwii 7 年 前
コミット
bbc9fe8233
1 ファイル変更6 行追加3 行削除
  1. 6 3
      xiaoiceapi.py

+ 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