|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
报错信息是什么意思?需要如何改?
先谢过各位了。
代码如下:
- import urllib.request
- import os
- import random
- def url_open(url):
- #隐藏:请求url并修改User-Agent
- 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/48.0.2564.97 Safari/537.36')
- #生成代理
- proxies = ['61.191.41.130:80']
- proxy = random.choice(proxies)
- #生成ProxyHandler,创建opener,安装opener
- proxy_support = urllib.request.ProxyHandler({'http':proxy})
- opener = urllib.request.build_opener(proxy_support)
- urllib.request.install_opener(opener)
- #请求打开url
- 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)
- #返回page的编号
- return html[a:b]
- def find_imgs(url):
- html = url_open(url).decode('utf-8')
- img_addrs = []
- #找到图片的链接
- a = html.find('img src=')
- #无链接时为-1
- while a!=-1:
- b = html.find('.jpg',a,a+255)
- # 当链接不是以jpg结尾时b为-1
- 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):
- #创建foler并进入folder
- 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()
复制代码
报错如下:
C:\Users\c\AppData\Local\Programs\Python\Python35\python.exe D:/Python视频/test/test1.py
Traceback (most recent call last):
File "D:/Python视频/test/test1.py", line 72, in <module>
download_mm()
File "D:/Python视频/test/test1.py", line 69, in download_mm
save_imgs(folder, img_addrs)
File "D:/Python视频/test/test1.py", line 54, in save_imgs
img = url_open(each)
File "D:/Python视频/test/test1.py", line 7, in url_open
req = urllib.request.Request(url)
File "C:\Users\c\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 269, in __init__
self.full_url = url
File "C:\Users\c\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 295, in full_url
self._parse()
File "C:\Users\c\AppData\Local\Programs\Python\Python35\lib\urllib\request.py", line 324, in _parse
raise ValueError("unknown url type: %r" % self.full_url)
ValueError: unknown url type: '//wx4.sinaimg.cn/mw600/0063qhYBgy1ffsu4e7ig7j30go0m8403.jpg'
Process finished with exit code 1
- img_addrs.append(html[a+9:b+4])
- 改成
- img_addrs.append('http:'+html[a+9:b+4])
- 地址就对了
复制代码
|
|