Practice hand with reptiles acquisition of urllib

Practice hand with reptiles acquisition of urllib

有个人看一段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)

Modified code 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))

Guess you like

Origin www.cnblogs.com/pythonywy/p/11326228.html