|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本人小白,按照小甲鱼视频敲出来的代码,报错:unknown url type,请大神们指点指点。
代码如下:
import urllib.request
import os
def url_open(url):
data=None
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.75 Safari/537.36 LBBROWSER')
response = urllib.request.urlopen(req)
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)
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'
img_addrs = find_imgs(page_url)
save_imgs(folder,img_addrs)
if __name__ == '__main__':
download_mm()
报错如下:
Traceback (most recent call last):
File "I:\python_work\pachong\download_mm.py", line 62, in <module>
download_mm()
File "I:\python_work\pachong\download_mm.py", line 59, in download_mm
save_imgs(folder,img_addrs)
File "I:\python_work\pachong\download_mm.py", line 44, in save_imgs
img = url_open(each)
File "I:\python_work\pachong\download_mm.py", line 6, in url_open
req = urllib.request.Request(url)
File "H:\Users\TDuser\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 329, in __init__
self.full_url = url
File "H:\Users\TDuser\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 355, in full_url
self._parse()
File "H:\Users\TDuser\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 384, in _parse
raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '//wx3.sinaimg.cn/crop.0.0.1000.562.1000/0070w64cly1fnxpklustgj30rs0ii768.jpg'
实在找不出原因,求指教 |
|