redaiconglin 发表于 2022-4-10 21:20:19

老师请帮我看看这个错误如何解决

import urllib.request
import os
def get_page(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36')
    response = urllib.request.urlopen(req)
    html = response.read().decode('utf-8')
    a = html.find('thisclass __web-inspector-hide-shortcut__')+42
    b = html.find('<',a)
    print(html)                                       想要看一下图片页面的地址是否正确,但是显示编码错误。请老师帮我看看如何解决谢谢了
def find_img (url):
    pass
def save_img(name,img_addrs):
    pass
def download_mm(name='tupian',pages = 10):
    os.mkdir(name)
    os.chdir(name)
    url = 'https://www.tupianzj.com/meinv/xinggan'
    page_num = int(get_page(url))
    for i in range(pages):
      page_num -= i
      page_url = url +'list_176_'+str(page_num)+'.html'
      img_addrs = find_img (page_url)
      save_img(name,img_addrs)
if __name__ == '__main__':
    download_mm()
目前代码还未写完只是想先看一下图片页面的数字提取的是不是正确。但显示编码错误。


ba21 发表于 2022-4-10 21:54:43

import cchardet
import cchardet
import urllib.request
import os

def get_page(url):
    req = urllib.request.Request(url)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36')
    response = urllib.request.urlopen(req)
    html = response.read()
   
    enc = cchardet.detect(html)
    enc = enc['encoding']
    html = html.decode(enc)
    print(html)

    a = html.find('thisclass __web-inspector-hide-shortcut__')
    b = html.find('<',a)
    print(html)                                       
def find_img (url):
    pass
def save_img(name,img_addrs):
    pass
def download_mm(name='tupian',pages = 10):
    os.mkdir(name)
    os.chdir(name)
    url = 'https://www.tupianzj.com/meinv/xinggan'
    page_num = int(get_page(url))
    for i in range(pages):
      page_num -= i
      page_url = url +'list_176_'+str(page_num)+'.html'
      img_addrs = find_img (page_url)
      save_img(name,img_addrs)
if __name__ == '__main__':
    download_mm()

redaiconglin 发表于 2022-4-10 22:24:56

ba21 发表于 2022-4-10 21:54
import cchardet

感谢老师
页: [1]
查看完整版本: 老师请帮我看看这个错误如何解决