鱼C论坛

 找回密码
 立即注册
查看: 1432|回复: 4

[已解决]Python 第56课 一直遇到404错误 不知问题在哪儿

[复制链接]
发表于 2017-8-1 21:55:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import urllib.request
  2. import sys
  3. import os


  4. def url_open(url):
  5.     req = urllib.request.Request(url)
  6.     req.add_header('User-Agent',
  7.                    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36')
  8.     resp = urllib.request.urlopen(url)
  9.     html = resp.read()
  10.     # print(html)
  11.     return html


  12. def get_page(url):  # 获取图片页码
  13.     html = url_open(url).decode('utf-8')  # 解码,之后查找图片页码做准备
  14.     a = html.find('current-comment-page') + 23  # 返回页码数的首位置
  15.     b = html.find(']', a)  # 返回页码数的末位置
  16.     # print(html[a:b])
  17.     return html[a:b]  # 拼接返回页码


  18. def find_imgs(url):  # 在当前URL查找图片
  19.     html = url_open(url).decode('utf-8')
  20.     img_addrs = []

  21.     a = html.find('img src=')  # 图片真实地址

  22.     while a != -1:
  23.         b = html.find('.jpg', a, a + 200)
  24.         if b != -1:
  25.             img_addrs.append(html[a + 9:b + 4])
  26.         else:
  27.             b = a + 9

  28.         a = html.find('img src=', b)
  29.     return img_addrs


  30. def save_imgs(img_addrs):
  31.     for each in img_addrs:
  32.         filename = each.split('/')[-1]
  33.         with open(filename, 'wb') as f:
  34.             img = url_open(each)
  35.             f.write(img)


  36. def download(folder='Girls', pages=20):
  37.     os.mkdir(folder)  # 创建文件夹
  38.     os.chdir(folder)  # 切换到文件夹目录

  39.     url = 'http://jandan.net/ooxx'
  40.     page_num = int(get_page(url))  # 获取当前该图片页码

  41.     for i in range(pages):  # 依次下载指定页数的图片
  42.         page_num -= 1
  43.         page_url = url + 'page-' + str(page_num) + '#comments'  # 图片的URL
  44.         # print(page_url)
  45.         img_addrs = find_imgs(page_url)
  46.         save_imgs(img_addrs)


  47. if __name__ == '__main__':
  48.     download()
复制代码


错误原因如下:
  1. Traceback (most recent call last):
  2.   File "C:\Program Files\JetBrains\PyCharm 2017.1.4\helpers\pydev\pydev_run_in_console.py", line 78, in <module>
  3.     globals = run_file(file, None, None)
  4.   File "C:\Program Files\JetBrains\PyCharm 2017.1.4\helpers\pydev\pydev_run_in_console.py", line 35, in run_file
  5.     pydev_imports.execfile(file, globals, locals)  # execute the script
  6.   File "C:\Program Files\JetBrains\PyCharm 2017.1.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
  7.     exec(compile(contents+"\n", file, 'exec'), glob, loc)
  8.   File "E:/Python_Learn/PycharmProjects/Spider_Test/DownloadMMPic.py", line 65, in <module>
  9.     download()
  10.   File "E:/Python_Learn/PycharmProjects/Spider_Test/DownloadMMPic.py", line 60, in download
  11.     img_addrs = find_imgs(page_url)
  12.   File "E:/Python_Learn/PycharmProjects/Spider_Test/DownloadMMPic.py", line 25, in find_imgs
  13.     html = url_open(url).decode('utf-8')
  14.   File "E:/Python_Learn/PycharmProjects/Spider_Test/DownloadMMPic.py", line 10, in url_open
  15.     resp = urllib.request.urlopen(url)
  16.   File "C:\Users\imwoo\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
  17.     return opener.open(url, data, timeout)
  18.   File "C:\Users\imwoo\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
  19.     response = meth(req, response)
  20.   File "C:\Users\imwoo\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
  21.     'http', request, response, code, msg, hdrs)
  22.   File "C:\Users\imwoo\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
  23.     return self._call_chain(*args)
  24.   File "C:\Users\imwoo\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
  25.     result = func(*args)
  26.   File "C:\Users\imwoo\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
  27.     raise HTTPError(req.full_url, code, msg, hdrs, fp)
  28. urllib.error.HTTPError: HTTP Error 404: Not Found
复制代码
最佳答案
2017-8-1 22:57:21
2017-08-01_225507.png
2017-08-01_225531.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-1 21:56:33 | 显示全部楼层
404不关你的事吧,要么是url错了,要么网站出问题了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-1 22:08:50 | 显示全部楼层
老甲鱼与小甲鱼 发表于 2017-8-1 21:56
404不关你的事吧,要么是url错了,要么网站出问题了
  1. get_page(url)
复制代码
没有出错,也能正确print(html)
但是在
  1. find_imgs()
复制代码
中就会出错,也不能print(html).
  1. import urllib.request

  2. def url_open(url):
  3.     req = urllib.request.Request(url)
  4.     req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36')
  5.     resp = urllib.request.urlopen(url)
  6.     html = resp.read()
  7.     # print(html)
  8.     return html

  9. def find_imgs():  # 在当前URL查找图片
  10.     html = url_open('http://jandan.net/pic/page-303#comments').decode('utf-8')
  11.     print(html)
  12.     # img_addrs = []
  13.     # a = html.find('img src=')  # 图片真实地址
  14.     #
  15.     # while a != -1:
  16.     #     b = html.find('.jpg', a, a + 200)
  17.     #     if b != -1:
  18.     #         img_addrs.append(html[a + 9:b + 4])
  19.     #     else:
  20.     #         b = a + 9
  21.     #     a = html.find('img src=', b)
  22.     # return img_addrs
复制代码

单独运行find_imgs()方法也出错
错误原因一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-1 22:57:21 | 显示全部楼层    本楼为最佳答案   
2017-08-01_225507.png
2017-08-01_225531.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-3 13:46:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-23 11:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表