鱼C论坛

 找回密码
 立即注册
查看: 1618|回复: 2

[已解决]参考各路写了一个爬虫的代码,然而我怎么改都报AttributeError

[复制链接]
发表于 2020-7-21 16:42:40 | 显示全部楼层 |阅读模式

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

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

x
代码如下:
  1. import base64
  2. import urllib.request
  3. import os
  4. import time
  5. import re

  6. def get_pageurl(page,nums):
  7.     url_list = []
  8.     for i in range(nums):
  9.         temp1 = time.strftime('%Y%mYd',time.gmtime()) #获取当天的日期
  10.         temp2 = temp1 + str(int(page)-i) #获取页数
  11.         temp3 = base64.b64encode(temp2.encode()).decode() #base64转码
  12.         url = 'http://jandan.com/ooxx/' + temp3 + '#comments'
  13.         url_list.append(url)

  14.     return url_list

  15. def get_html(url):
  16.     # 伪装电脑查询
  17.     req = urllib.request.Request(url)
  18.     req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0")
  19.     response = urllib.requset.urlopen(req)
  20.     html = response.read()
  21.    
  22.     return html


  23. def get_img(html):
  24.     html = html.decode('utf-8')
  25.     img_list = [] #创建列表用来保存图片地址

  26.     a = r'<img src="([^]+\.(jpg|gif))"' #使用正则表达式获取图片地址
  27.     img = re.findall(a,html)
  28.    
  29.     #将每个地址塞入列表
  30.     for each in img:
  31.         img_list.append(each)

  32.     return img_list

  33. def save_imgs(img_list):
  34.     for each in img_list:
  35.         img = get_html(each)
  36.         filename = each.split('/')[-1]
  37.         with open(filename,'wb') as f:
  38.             imge  = open_url(each)
  39.             f.write(imge)
  40.         

  41. def download_mm():
  42.     os.mkdir('mmphoto')
  43.     os.chdir('mmphoto')
  44.     '''
  45.     x = int(input('请输入开始页码:'))
  46.     y = int(input('请输入下载多少页的图片:'))
  47.     '''
  48.     url_list = get_pageurl(130,2)
  49.     print(url_list)
  50.     for each in url_list:
  51.         html = get_html(each)
  52.         img_list = get_img(html)
  53.         save_imgs(img_list)
  54.         time.sleep(2)
  55.         print(each)

  56. if __name__ == '__main__':
  57.     download_mm()
复制代码


报错如下:
  1. Traceback (most recent call last):
  2.   File "E:\新建文件夹\爬取煎蛋网妹子图.py", line 67, in <module>
  3.     download_mm()
  4.   File "E:\新建文件夹\爬取煎蛋网妹子图.py", line 60, in download_mm
  5.     html = get_html(each)
  6.   File "E:\新建文件夹\爬取煎蛋网妹子图.py", line 22, in get_html
  7.     response = urllib.requset.urlopen(req)
  8. AttributeError: module 'urllib' has no attribute 'requset'
复制代码


求大佬提出下问题所在
最佳答案
2020-7-21 16:48:02


是 request 不是 requset

而且你代码有个  open_url 不知道从何而来,代码中没有定义这个函数呀

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-21 16:48:02 | 显示全部楼层    本楼为最佳答案   


是 request 不是 requset

而且你代码有个  open_url 不知道从何而来,代码中没有定义这个函数呀

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-21 16:49:06 | 显示全部楼层
本帖最后由 xiaosi4081 于 2020-7-21 16:53 编辑
  1. import base64
  2. import urllib.request
  3. import os
  4. import time
  5. import re

  6. def get_pageurl(page,nums):
  7.     url_list = []
  8.     for i in range(nums):
  9.         temp1 = time.strftime('%Y%mYd',time.gmtime()) #获取当天的日期
  10.         temp2 = temp1 + str(int(page)-i) #获取页数
  11.         temp3 = base64.b64encode(temp2.encode()).decode() #base64转码
  12.         url = 'http://jandan.com/ooxx/' + temp3 + '#comments'
  13.         url_list.append(url)

  14.     return url_list

  15. def get_html(url):
  16.     # 伪装电脑查询
  17.     req = urllib.request.Request(url)
  18.     req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0")
  19.     response = urllib.request.urlopen(req)
  20.     html = response.read()
  21.    
  22.     return html


  23. def get_img(html):
  24.     html = html.decode('utf-8')
  25.     img_list = [] #创建列表用来保存图片地址

  26.     a = r'<img src="([^]+\.(jpg|gif))"' #使用正则表达式获取图片地址
  27.     img = re.findall(a,html)
  28.    
  29.     #将每个地址塞入列表
  30.     for each in img:
  31.         img_list.append(each)

  32.     return img_list

  33. def save_imgs(img_list):
  34.     for each in img_list:
  35.         img = get_html(each)
  36.         filename = each.split('/')[-1]
  37.         with open(filename,'wb') as f:
  38.             imge  = open_url(each)
  39.             f.write(imge)
  40.         

  41. def download_mm():
  42.     os.mkdir('mmphoto')
  43.     os.chdir('mmphoto')
  44.     '''
  45.     x = int(input('请输入开始页码:'))
  46.     y = int(input('请输入下载多少页的图片:'))
  47.     '''
  48.     url_list = get_pageurl(130,2)
  49.     print(url_list)
  50.     for each in url_list:
  51.         html = get_html(each)
  52.         img_list = get_img(html)
  53.         save_imgs(img_list)
  54.         time.sleep(2)
  55.         print(each)

  56. if __name__ == '__main__':
  57.     download_mm()
复制代码


还有,你这个open_url从何处来?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-24 01:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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