有个人看一段python2的代码有很多错误

import re
import urllib

def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

def getImg(html):
    reg = r'src="(.+?\.jpg)" pic_ext'
    imgre = re.compile(reg)
    imglist = re.findall(imgre,html)
    return imglist 

html = getHtml("https://zwk365.com") //攒外快网
print getImg(html)

修改后python3的代码

import re
import urllib.request

def getHtml(url):
    page = urllib.request.urlopen(url) #获取网站
    html = page.read() #内容读取,返回的html是字节的格式
    return html

def getImg(html):
    # print(str(html,encoding='utf8'))   #内容以爬下来为准而不是网站上的
    reg = 'data-original="(.*?)"'  #设置下内容的re格式
    imglist = re.findall(reg,str(html,encoding='utf8'),re.S)
    return imglist

html = getHtml("https://zwk365.com")
print(getImg(html))

相关文章:

  • 2022-01-10
  • 2022-12-23
  • 2021-05-09
  • 2021-09-24
  • 2021-12-02
  • 2021-08-26
猜你喜欢
  • 2021-08-19
  • 2021-08-30
  • 2021-10-09
  • 2022-01-25
  • 2021-06-10
  • 2021-06-01
  • 2021-12-18
相关资源
相似解决方案