python urllib2.quote()报错Invalid percent-escape sequence?

背景:在写一个自动去射手网爬字幕的字幕搜索脚本

代码如下

import sys                                                                                               
import urllib2                                                                                           
import requests                                                                                          
import re                                                                                                
reload(sys)                                                                                              
sys.setdefaultencoding("utf-8")                                                                          
                                                                                                     
def download(searchname):                                                                                
    header = {                                                                                           
            'Host':'sub.makedie.me',                                                                     
            'Pragma':'no-cache',                                                                         
            'Referer':'http://sub.makedie.me/',                                                          
            'Upgrade-Insecure-Requests':'1',                                                             
            'User-Agent':'Mozilla/5.0 (X11; Linux x86_64)     AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36'}
    payload = {'searchword' : searchname}                                                                
    url = 'http://sub.makedie.me/sub/'                                                                      
    s = requests.Session()                                                                                  
    response = s.get(url,params=payload,headers=header)                                                     
    if response.status_code == requests.codes.ok:                                                           
        file = response.text                                                                                
    return file                                                                                             
                                                                                                        
                                                                                                        
                                                                                                        
file_temp = open('filetemp.txt','w')                                                                        
file_temp.write(download(urllib2.quote("绿箭侠 第三季 第13集/Arrow.S03E13.720p.HDTV.X264-DIMENSION.chn")))       

出现问题:

 raise InvalidURL("Invalid percent-escape sequence: '%s'" % h)
 requests.exceptions.InvalidURL: Invalid percent-escape sequence: 'DI'

检查之后去掉绿箭侠 第三季 第13集/Arrow.S03E13.720p.HDTV.X264-DIMENSION.chn中'DI'前面的-后报错消失.

求教产生这种错误的原因,或者有什么比较好的替代方法,新人第一次提问,有什么问题大家指出,虚心接受大家的批评~

阅读 4.1k
2 个回答
response = s.get(url,params=payload,headers=header) 

妀为:

response = s.get(url+params,headers=header) 

概念混淆了。

看这个 url: http://www.baidu.com/bitches?action=fuckyou

其中action=fuckyou才是params,而/bitches则叫path,你提供的显然是path。

无意黑百毒。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题