|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ff174422523 于 2018-3-4 23:53 编辑
大家好,现我按照小甲鱼老师的方法下载妹子图时报错,报错内容与代码如下,还请指导,谢谢。,视频课程“第56课,论一只爬虫的自我修养4:OOXX”,
现在执行代码时报错如下
Traceback (most recent call last):
File "C:/Users/Misq/AppData/Local/Programs/Python/Python36/jiandan.py", line 54, in <module>
download_mm()
File "C:/Users/Misq/AppData/Local/Programs/Python/Python36/jiandan.py", line 45, in download_mm
page_num = int(get_page(url))
File "C:/Users/Misq/AppData/Local/Programs/Python/Python36/jiandan.py", line 13, in get_page
html = url_open(url).decode('utf-8')
File "C:/Users/Misq/AppData/Local/Programs/Python/Python36/jiandan.py", line 7, in url_open
response = urllib.request.urlopen(url)
File "C:\Users\Misq\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\Misq\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 532, in open
response = meth(req, response)
File "C:\Users\Misq\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 642, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Users\Misq\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 570, in error
return self._call_chain(*args)
File "C:\Users\Misq\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "C:\Users\Misq\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 650, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
我的代码如下
import urllib.request
import os
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0')
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[a:b]
def find_imgs(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(html[a+9:b+4])
else:
b = a + 9
a = html.find('img src=',b)
def save_imgs(folder,img_addrs):
pass
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'
img_addrs = find_imgs(page_url)
save_imgs(folder,img_addrs)
if __name__ == '__main__':
download_mm()
|
|