鱼C论坛

 找回密码
 立即注册
查看: 803|回复: 5

报了个错误,求大佬解答

[复制链接]
发表于 2018-10-19 10:55:23 | 显示全部楼层 |阅读模式

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

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

x
  1. import urllib.request
  2. import os

  3. def get_url(url):

  4.     headers = {
  5.         'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
  6.     }
  7.     req = urllib.request.Request(url,headers=headers)
  8.     response = urllib.request.urlopen(req)
  9.     html = response.read()
  10.     return html


  11. def url_img(url):
  12.     img_addrs = []
  13.     html = get_url(url).decode('utf-8')
  14.     a = html.find('img src2=')
  15.     while a != -1:
  16.         b = html.find('.jpg', a, a + 255)
  17.         if b != -1:
  18.             img_addrs.append(html[a:b] + '.jpg')
  19.         else:
  20.             print('找不到图片地址')
  21.         a = html.find('img src2=',b)
  22.     return img_addrs
  23.     #print(url_img(url))                              #获取列表内页面第一页所有图片地址
  24. #print(len(url_img(url)))                         #列表内有多少图片

  25. def save_imgs(folder,img_addrs):

  26.     for each in img_addrs:
  27.         img_url = each.split('"')[1]
  28.         with open(img_url,'wb') as f:
  29.             img = get_url(each)
  30.             f.write(img)

  31. def download_mm(folder='katong',pages=10):
  32.     os.mkdir(folder)
  33.     os.chdir(folder)

  34.     url = 'http://sc.chinaz.com/tupian/katongtupian_2.html'

  35.     for i in range(1,pages):
  36.         i += 1
  37.         get_img = 'http://sc.chinaz.com/tupian/katongtupian' + '_' + str(i) + '.html'
  38.         img_addrs = url_img(get_img)
  39.         save_imgs(folder,img_addrs)


  40. if __name__ == '__main__':
  41.     download_mm()



  42. #错误
  43. Traceback (most recent call last):
  44.   File "C:/Users/Administrator/Desktop/测试.py", line 52, in <module>
  45.     download_mm()
  46.   File "C:/Users/Administrator/Desktop/测试.py", line 48, in download_mm
  47.     save_imgs(folder,img_addrs)
  48.   File "C:/Users/Administrator/Desktop/测试.py", line 34, in save_imgs
  49.     with open(img_url,'wb') as f:
  50. OSError: [Errno 22] Invalid argument: 'http://pic.sc.chinaz.com/Files/pic/pic9/201806/zzpic12352_s.jpg'
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-10-19 10:56:50 | 显示全部楼层
文件名要处理的
split('/')[-1]获得文件名,你这整整一个url
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 11:00:12 | 显示全部楼层
open方法无法打开一个网页。  
用你的打开网页函数打开这个地址。  
with open打开一个文件,把你打开网页函数的内容写到这个open函数的参数中
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 11:06:39 | 显示全部楼层
塔利班 发表于 2018-10-19 10:56
文件名要处理的
split('/')[-1]获得文件名,你这整整一个url

行吧,不爬了还是在学学吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 11:07:31 | 显示全部楼层
wongyusing 发表于 2018-10-19 11:00
open方法无法打开一个网页。  
用你的打开网页函数打开这个地址。  
with open打开一个文件,把你打开网 ...

行吧,爬这个好麻烦还是去爬些简单的吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 11:10:30 | 显示全部楼层
923204485 发表于 2018-10-19 11:07
行吧,爬这个好麻烦还是去爬些简单的吧

你离成功只差一步。这个网站很好爬啊。  
真的差一步,打开图片地址,获取里面的内容。  
写入
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-13 19:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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