由于python目前不能直接处理中文路径,必须要转化一下,如下例子是下载图片(名字为中文的):
def getInfo(self,imageurl):
response = urllib.request.urlopen(imageurl).read().decode(‘utf-8‘)
# with open("text1.txt",‘w‘,encoding=‘utf-8‘) as file:
# file.write(response)
# file.close()
imageRe = re.compile(r‘<a href=\"(.+)\" title‘)
for image in imageRe.findall(response):
pattern = re.compile(r‘^(http://.+/)(.+[jpg|JPG])$‘)
matchUrl = pattern.match(image)
if matchUrl:
‘由于Python不能解析中文路径,所以quote一下中文字符‘
imagePath = urllib.parse.urljoin(matchUrl.group(1), urllib.parse.quote(matchUrl.group(2)))
self.count =self.count+1
path = matchUrl.group(2)
print(imageurl)
print(image)
‘save picture‘
urllib.request.urlretrieve(imagePath, path)
def getLink(self,url):
response = urllib.request.urlopen(url).read().decode(‘utf-8‘)
linkRe = re.compile(r‘href="(http://.+\d+)" title=‘)
for link in linkRe.findall(response):
self.getInfo(link)
# with open("text.txt",‘w‘,encoding=‘utf-8‘) as file:
# file.write(response)
# file.close()
python3 网址路径中带中文的处理办法,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/orientwolf/p/3857487.html