热枕。 2019-08-06 00:35 采纳率: 0%
浏览 389

这个代码报错怎么解决?

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

  • 写回答

2条回答 默认 最新

  • 毕小宝 博客专家认证 2019-08-06 09:28
    关注

    imglist = re.findall(imgre.html)
    这句 imgre 没有 html 属性,应该还是用法错误了。找找相关的 API 在看看:
    https://www.cnblogs.com/papapython/p/7482349.html

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料