首页 > 代码库 > python3 网址路径中带中文的处理办法

python3 网址路径中带中文的处理办法

由于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=http://www.mamicode.com/"(.+)\" 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://www.mamicode.com/(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()