想下载照片报错

import urllib.request 
import re 
  
 
def saveFile(data):  
    path = '/Users/hckj/Desktop/python_pic_test/test1.txt' 
    f = open(path,'wb')  
    f.write(data)  
    f.close()  

def saveDetailPics(data):
     path = '/Users/hckj/Desktop/python_pic_test/' 
     reg = r'src="(.+?\quality/90)"'
     imgre = re.compile(reg)
     imglist = re.findall(imgre, data)
     print (len(imglist))
     x = 0
     for imgurl in imglist:
         print (x)
         urllib.request.urlretrieve(imgurl,path%x)
         x = x+1
  
  
url = "http://www.wealoha.com/user/PBvQHwFF9fg?shareby=PBvQHwFF9fg"  
headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1)'}
req = urllib.request.Request(url=url,headers=headers)  
  
res = urllib.request.urlopen(req)  
  
data = res.read()  
   
  
data = data.decode('utf-8')  


saveDetailPics(data) 




然后报错

----

Traceback (most recent call last):
  File "demo1.py", line 39, in <module>
    saveDetailPics(data) 
  File "demo1.py", line 14, in saveDetailPics
    imgre = re.compile(reg)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 233, in compile
    return _compile(pattern, flags)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py", line 301, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_compile.py", line 562, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 855, in parse
    p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 416, in _parse_sub
    not nested and not items))
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 765, in _parse
    p = _parse_sub(source, state, sub_verbose, nested + 1)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 416, in _parse_sub
    not nested and not items))
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 502, in _parse
    code = _escape(source, this, state)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/sre_parse.py", line 401, in _escape
    raise source.error("bad escape %s" % escape, len(escape))
sre_constants.error: bad escape \q at position 9


哪里写错了...

阅读 3.4k
3 个回答

很明显啊,reg里的\q有问题

reg = r'src="(.+?\\quality/90)"'

import wget
from os import chdir

chdir('/Users/hckj/Desktop/python_pic_test/')
wget.download(url)

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