|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import urllib.request
- import os
- def url_open(url):
- req = urllib.request.Request(url)
- req.add_header('User-Agent', 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36')
- 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)
- 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(html[a+9:b+4])
- else:
- b=a+9
- a=html.find('img src=',b)
- for each in img_addrs:
- print(each)
- 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://www.mm131.com/"
- page_num=int(get_page(url))
- for i in range(pages):
- page_num -=i
- page_url =url+"qingchun/"+str(page_num)+'.html'
- img_addrs=find_imgs(page_url)
- save_imgs(folder,img_addrs)
- if __name__=='__main__':
- download_mm()
复制代码
以上是代码
C:\Users\admin\AppData\Local\Programs\Python\Python36\python.exe C:/Users/admin/PycharmProjects/untitled/.idea/爬取妹子图.py
Traceback (most recent call last):
File "C:/Users/admin/PycharmProjects/untitled/.idea/爬取妹子图.py", line 58, in <module>
download_mm()
File "C:/Users/admin/PycharmProjects/untitled/.idea/爬取妹子图.py", line 49, in download_mm
page_num=int(get_page(url))
File "C:/Users/admin/PycharmProjects/untitled/.idea/爬取妹子图.py", line 13, in get_page
html=url_open(url).decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 365: invalid continuation byte
这个是报错,搞不懂关utf-8什么事
在原网页通过F12查看编码方式,再对应解码就行了。
|
|