比如这段代码现在出现TypeError: cannot use a string pattern on a bytes-like object
import urllib.request
import re
def getHtmlContent(url):
page = urllib.request.urlopen(url)
return page.read()
def getJPGs(html):
jpgReg = re.compile(r'<img.+?src="(.+?\.jpg)" width')
jpgs = re.findall(jpgReg, html)
return jpgs
def downloadJPG(imgUrl, fileName):
urllib.request.urlretrieve(imgUrl, fileName)
def batchDownloadJPGs(imgUrls, path = 'D:\\科目\\冯骥 大数据处理与分析\\pic\\'):
count = 1
for url in imgUrls:
downloadJPG(url, ''.join([path, '{0}.jpg'.format(count)]))
print ('number'+str(count)+'page')
count = count + 1
def download(url):
html = getHtmlContent(url)
jpgs = getJPGs(html)
batchDownloadJPGs(jpgs)
def main():
url = 'http://tieba.baidu.com/p/2256306796'
download(url)
if __name__ == '__main__':
main()
TypeError: cannot use a string pattern on a bytes-like object
解决方法:
html用decode(‘utf-8’)进行解码,由bytes变成string
py3的urlopen返回的不是string是bytes
把’html’类型调整一下:html.decode(‘utf-8’)
再运行下代码就没问题了
import urllib.request
import re
def getHtmlContent(url):
page = urllib.request.urlopen(url)
return page.read()
def getJPGs(html):
jpgReg = re.compile(r'<img.+?src="(.+?\.jpg)" width')
jpgs = re.findall(jpgReg, html.decode('utf-8'))
return jpgs
def downloadJPG(imgUrl, fileName):
urllib.request.urlretrieve(imgUrl, fileName)
def batchDownloadJPGs(imgUrls, path = 'D:\\科目\\冯骥 大数据处理与分析\\pic\\'):
count = 1
for url in imgUrls:
downloadJPG(url, ''.join([path, '{0}.jpg'.format(count)]))
print ('number'+str(count)+'page')
count = count + 1
def download(url):
html = getHtmlContent(url)
jpgs = getJPGs(html)
batchDownloadJPGs(jpgs)
def main():
url = 'http://tieba.baidu.com/p/2256306796'
download(url)
if __name__ == '__main__':
main()