|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:
- import base64
- import urllib.request
- import os
- import time
- import re
- def get_pageurl(page,nums):
- url_list = []
- for i in range(nums):
- temp1 = time.strftime('%Y%mYd',time.gmtime()) #获取当天的日期
- temp2 = temp1 + str(int(page)-i) #获取页数
- temp3 = base64.b64encode(temp2.encode()).decode() #base64转码
- url = 'http://jandan.com/ooxx/' + temp3 + '#comments'
- url_list.append(url)
- return url_list
- def get_html(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/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0")
- response = urllib.requset.urlopen(req)
- html = response.read()
-
- return html
- def get_img(html):
- html = html.decode('utf-8')
- img_list = [] #创建列表用来保存图片地址
- a = r'<img src="([^]+\.(jpg|gif))"' #使用正则表达式获取图片地址
- img = re.findall(a,html)
-
- #将每个地址塞入列表
- for each in img:
- img_list.append(each)
- return img_list
- def save_imgs(img_list):
- for each in img_list:
- img = get_html(each)
- filename = each.split('/')[-1]
- with open(filename,'wb') as f:
- imge = open_url(each)
- f.write(imge)
-
- def download_mm():
- os.mkdir('mmphoto')
- os.chdir('mmphoto')
- '''
- x = int(input('请输入开始页码:'))
- y = int(input('请输入下载多少页的图片:'))
- '''
- url_list = get_pageurl(130,2)
- print(url_list)
- for each in url_list:
- html = get_html(each)
- img_list = get_img(html)
- save_imgs(img_list)
- time.sleep(2)
- print(each)
- if __name__ == '__main__':
- download_mm()
复制代码
报错如下:
- Traceback (most recent call last):
- File "E:\新建文件夹\爬取煎蛋网妹子图.py", line 67, in <module>
- download_mm()
- File "E:\新建文件夹\爬取煎蛋网妹子图.py", line 60, in download_mm
- html = get_html(each)
- File "E:\新建文件夹\爬取煎蛋网妹子图.py", line 22, in get_html
- response = urllib.requset.urlopen(req)
- AttributeError: module 'urllib' has no attribute 'requset'
复制代码
求大佬提出下问题所在
是 request 不是 requset
而且你代码有个 open_url 不知道从何而来,代码中没有定义这个函数呀
|
|