breaki 发表于 2020-8-1 16:11:15

python

import urllib.request
import re
response = urllib.request.urlopen('https://tieba.baidu.com/p/6812722163')
html = response.read().decode('utf-8')
p = r'<img class="BDE_Image".*?src="[^"]*\.jpg".*?>'
imglist = re.findall(p.html)
for each in imglist:
    print(each)
为什么会出现
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/新建文件夹/39.py", line 6, in <module>
    imglist = re.findall(p.html)
AttributeError: 'str' object has no attribute 'html'
这样的错误。

Twilight6 发表于 2020-8-1 16:19:49



这里 re.findall(p.html) 用成 . 了 ,改成 , 逗号即可 即 :re.findall(p,html)

正确代码:
import urllib.request
import re
response = urllib.request.urlopen('https://tieba.baidu.com/p/6812722163')
html = response.read().decode('utf-8')
p = r'<img class="BDE_Image".*?src="[^"]*\.jpg".*?>'
imglist = re.findall(p,html)
for each in imglist:
    print(each)
页: [1]
查看完整版本: python