import urllib.request
import re
def getHtml(url):
response = urllib.request.urlopen(url)
html = response.read()
return html
# (2)下面代码获取帖子内所有图片地址
def getImg(html):
reg = r'src="([.*\s]*\.jpg)" pic_ext="jpeg"'
imgre = re.compile(reg)
imglist = re.findall(imgre.html)
return imglist
# (3)使用getHtml()输入任意帖子的URL地址
html = getHtml("http://tieba.baidu.com/p/3205263090")
# (4)修改html对象内的字符编码为UTF-8
html = html.decode('UTF-8')
# (5)使用下面代码循环保存图片
imgList = getImg(html)
print(imgList)
报错:D:\python\项目储存\untitled\venv\Scripts\python.exe D:/python/项目储存/untitled/dome.py
Traceback (most recent call last):
File "D:/python/项目储存/untitled/dome.py", line 21, in
imgList = getImg(html)
File "D:/python/项目储存/untitled/dome.py", line 12, in getImg
imglist = re.findall(imgre.html)
AttributeError: 're.Pattern' object has no attribute 'html'
Process finished with exit code 1
imglist = re.findall(imgre.html)
这句 imgre 没有 html 属性,应该还是用法错误了。找找相关的 API 在看看:
https://www.cnblogs.com/papapython/p/7482349.html
你应该对正则表达式还不熟悉, 你的正则表达式代码有问题.
1. 如果你用compile方法, 正确写法是:
reg = r'src="([.*\s]*.jpg)" pic_ext="jpeg"'
imgre = re.compile(reg)
imglist = imgre.findall(html) # 不清楚你原来的imgre.html是什么意思, 这也是报错原因,即正则匹配对象imgre没有html属性
2 . 如果一步完成:
re.findall( r'src="([.*\s]*.jpg)" pic_ext="jpeg"', html)
建议再看下正则表达式用法,我想你可能是将re.findall和compile的pattern.findall方法弄混了.