比如这段代码现在出现AttributeError: module ‘urllib’ has no attribute ‘urlopen’
import urllib
import re
def getHtmlContent(url):
page = urllib.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.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()
AttributeError: module 'urllib' has no attribute 'urlopen'
解决方法:
Python3.X中应该用urllib.request
将urllib改为urllib.request
再运行下代码就没问题了
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()