|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
煎蛋网爬取图片不成功该怎么解决?
代码如下:
- 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('http:' + 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)
- time.sleep(10)
- if __name__ == '__main__':
- download_mm()
复制代码
报错如下
Traceback (most recent call last):
File "E:\Python学习\小甲鱼课上\下载妹子图.py", line 63, in <module>
download_mm()
File "E:\Python学习\小甲鱼课上\下载妹子图.py", line 58, in download_mm
img_addrs = find_imgs(page_url)
File "E:\Python学习\小甲鱼课上\下载妹子图.py", line 23, in find_imgs
html = url_open(url).decode('utf-8')
File "E:\Python学习\小甲鱼课上\下载妹子图.py", line 8, in url_open
response = urllib.request.urlopen(req)
File "E:\python\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "E:\python\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "E:\python\lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "E:\python\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "E:\python\lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "E:\python\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found
现在煎蛋网不是直接拿数字当页码了,建议你学了正则然后去爬链接吧,这里举个爬一个图库的例子:
- import re
- import urllib.request
- import os
- def open_url(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.request.urlopen(req)
- html = response.read()
- return html
- def get_img(html): # 获取图片
- p = r'<img src="([^"]+\.jpg)'
- imglist = re.findall(p, html.decode('utf-8', "replace"))
- '''
- for each in imglist:
- print(each)
- '''
- count = 0
- for each in imglist:
- count += 1
- filename = '%d.jpg'
- pic = 'http:' + each
- with open(filename % count, 'wb') as f:
- img = open_url(pic)
- f.write(img)
- if __name__ == '__main__':
- url = 'http://jandan.net/ooxx/MjAyMDAxMDMtNjk=#comments'
- get_img(open_url(url))
复制代码
|
|