哪位行行好帮我看看我该怎么办
import urllib.requestimport os
def url_open(url):
req = urllib.requests.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
response = urllib.request.urlopen
html = response.read()
return html
def get_page(url):
html = url_open(url).decode(utf-8)
a = html.find('current-comment-page')+23
b = html.find(']',a)
return html
def find_img(url):
html = url_open(url).decode(utf-8)
img_addrs = []
a = html.find('img src = ')
while a != -1:
b = html.find('.jpg',a,a+255)
if b != -1:
img_addrs.append('https:'+html)
else:
b = a + 9
a = html.find('img src = ',b)
return img_addrs
def save_imgs(folder,img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename,'wb')as f:
img = url_open(each)
f.write(img)
def download_mm(folder='ooxx',pages=10):
os.mkdir(folder)
os.chdir(folder)
url = 'http://jandan.net/ooxx/'
page_num = int(get_page)
for i in range(pages):
page_num -= i
page_url = url + 'page-' + str(page_num) + '#comments'
find_addrs = find_imgs(page_url)
save_imgs(img_addrs)
if __name__ == "__main__":
download_mm()
Traceback (most recent call last):
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 60, in <module>
download_mm()
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 51, in download_mm
page_num = int(get_page)
TypeError: 'function' object is not subscriptable
import urllib.request
import os
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 '
'Safari/537.36')
response = urllib.request.urlopen
html = response.read()
return html
def get_page(url):
html = url_open(url).decode("utf-8")
a = html.find('current-comment-page') + 23
b = html.find(']', a)
return html
def find_img(url):
html = url_open(url).decode("utf-8")
img_addrs = []
a = html.find('img src = ')
while a != -1:
b = html.find('.jpg', a, a + 255)
if b != -1:
img_addrs.append('https:' + html)
else:
b = a + 9
a = html.find('img src = ', b)
return img_addrs
def save_imgs(folder, img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename, 'wb')as f:
img = url_open(each)
f.write(img)
def download_mm(folder='ooxx', pages=10):
os.mkdir(folder)
os.chdir(folder)
url = 'http://jandan.net/ooxx/'
page_num = int(get_page(url))
for i in range(pages):
page_num -= i
page_url = url + 'page=' + str(page_num) + '#comments'
find_addrs = find_img(page_url)
save_imgs(folder, find_addrs)
if __name__ == "__main__":
download_mm() 把中括号改成小括号 BngThea 发表于 2020-4-1 20:18
把中括号改成小括号
大神说的是什么位置的中括号? zltzlt 发表于 2020-4-1 20:18
我把您的代码复制进去了还是报的TypeError: 'function' object is not subscriptable TypeError: 'function' object is not subscriptable
返回信息告诉你函数对象不能用下标索引
回到你错误的行可以看到
page_num = int(get_page)
get_page【url】这里你用的是中括号【】,表示索引呢
改成小括号 十月故里 发表于 2020-4-1 20:38
TypeError: 'function' object is not subscriptable
返回信息告诉你函数对象不能用下标索引
回到你错误 ...
听您改了,然后报了这个错module 'urllib' has no attribute 'requests' CD84380973 发表于 2020-4-1 20:43
听您改了,然后报了这个错module 'urllib' has no attribute 'requests'
request多打了个s 十月故里 发表于 2020-4-1 20:38
TypeError: 'function' object is not subscriptable
返回信息告诉你函数对象不能用下标索引
回到你错误 ...
import urllib.request
import os
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
response = urllib.request.urlopen
html = response.read()
return html
def get_page(url):
html = url_open(url).decode(utf-8)
a = html.find('current-comment-page')+23
b = html.find(']',a)
return html
def find_img(url):
html = url_open(url).decode(utf-8)
img_addrs = []
a = html.find('img src = ')
while a != -1:
b = html.find('.jpg',a,a+255)
if b != -1:
img_addrs.append('https:'+html)
else:
b = a + 9
a = html.find('img src = ',b)
return img_addrs
def save_imgs(folder,img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename,'wb')as f:
img = url_open(each)
f.write(img)
def download_mm(folder='ooxx',pages=10):
os.mkdir(folder)
os.chdir(folder)
url = 'http://jandan.net/ooxx/'
page_num = int(get_page(url))
for i in range(pages):
page_num -= i
page_url = url + 'page-' + str(page_num) + '#comments'
find_addrs = find_imgs(page_url)
save_imgs(img_addrs)
if __name__ == "__main__":
download_mm()
您说的地方我改了,第六行request多打了s也去了,结果还是报TypeError: 'function' object is not subscriptable您帮我看看还有哪里用错了中括号呢 Hoiste 发表于 2020-4-1 20:49
request多打了个s
这两处改了还是报的TypeError: 'function' object is not subscriptable,难道还有哪里中括号用错了? 你把全部返回信息贴上来 十月故里 发表于 2020-4-1 20:50
你把全部返回信息贴上来
Traceback (most recent call last):
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 60, in <module>
download_mm()
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 51, in download_mm
page_num = int(get_page(url))
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 13, in get_page
html = url_open(url).decode(utf-8)
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 7, in url_open
response = urllib.request.urlopen
TypeError: 'function' object is not subscriptable 我看你第一个def url_open这里就有一个写错的[]
CD84380973 发表于 2020-4-1 20:50
这两处改了还是报的TypeError: 'function' object is not subscriptable,难道还有哪里中括号用错了?
发下完整错误信息吧,错误说明是用下标去访问不可以下标访问的对象了 十月故里 发表于 2020-4-1 20:52
我看你第一个def url_open这里就有一个写错的[]
我把那处改了,现在是
Traceback (most recent call last):
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 60, in <module>
download_mm()
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 51, in download_mm
page_num = int(get_page(url))
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 13, in get_page
html = url_open(url).decode(utf-8)
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 7, in url_open
response = urllib.request.urlopen(url)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden CD84380973 发表于 2020-4-1 20:36
大神说的是什么位置的中括号?
函数调用啊
你download_mm 中调用了get_page CD84380973 发表于 2020-4-1 20:52
Traceback (most recent call last):
File "C:%users\39436\Desktop\python练习\download_mm.py", lin ...
果然,你平时报错的时候可以看报错信息,error上面一行就是你最后报错的地方,然后根据报错信息来改正就好了,这里的问题还是错误的写成[]了,调用函数用的是(),索引之类才用[] Hoiste 发表于 2020-4-1 20:54
发下完整错误信息吧,错误说明是用下标去访问不可以下标访问的对象了
现在改了是这样了
import urllib.request
import os
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
response = urllib.request.urlopen(url)
html = response.read()
return html
def get_page(url):
html = url_open(url).decode(utf-8)
a = html.find('current-comment-page')+23
b = html.find(']',a)
return html
def find_img(url):
html = url_open(url).decode(utf-8)
img_addrs = []
a = html.find('img src = ')
while a != -1:
b = html.find('.jpg',a,a+255)
if b != -1:
img_addrs.append('https:'+html)
else:
b = a + 9
a = html.find('img src = ',b)
return img_addrs
def save_imgs(folder,img_addrs):
for each in img_addrs:
filename = each.split('/')[-1]
with open(filename,'wb')as f:
img = url_open(each)
f.write(img)
def download_mm(folder='ooxx',pages=10):
os.mkdir(folder)
os.chdir(folder)
url = 'http://jandan.net/ooxx/'
page_num = int(get_page(url))
for i in range(pages):
page_num -= i
page_url = url + 'page-' + str(page_num) + '#comments'
find_addrs = find_imgs(page_url)
save_imgs(img_addrs)
if __name__ == "__main__":
download_mm()
Traceback (most recent call last):
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 60, in <module>
download_mm()
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 51, in download_mm
page_num = int(get_page(url))
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 13, in get_page
html = url_open(url).decode(utf-8)
File "C:\Users\39436\Desktop\python练习\download_mm.py", line 7, in url_open
response = urllib.request.urlopen(url)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\39436\AppData\Local\Programs\Python\Python38\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden CD84380973 发表于 2020-4-1 20:55
我把那处改了,现在是
Traceback (most recent call last):
现在是403了,你的url要么有问题,要么对面禁止你访问了,你先用你自己浏览器打开下你的url看能不能正常打开,你这里应该是小甲鱼的网址,我记得好像网址变了 十月故里 发表于 2020-4-1 20:56
果然,你平时报错的时候可以看报错信息,error上面一行就是你最后报错的地方,然后根据报错信息来改正就 ...
越整越负了,我错了那么多行?
页:
[1]
2