最近学习了Python,借助各个大神的文章,自己写了以下代码,来爬取网页图片,希望可以帮助到大家。
工具是 idea
#coding=utf-8
import requests
from bs4 import BeautifulSoup
import os
import sys
'''
#安卓端需要此语句
reload(sys)
sys.setdefaultencoding('utf-8')
'''
if(os.name == 'nt'):
print(u'你正在使用win平台')
else:
print(u'你正在使用linux平台')
header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.1.2107.204 Safari/537.36'}
#http请求头
all_url = 'http://www.win4000.com/zt/xinggan.html'
start_html = requests.get(all_url,headers = header)
#保存地址 手动创建文件夹
path = 'D:/练习/'
#找寻最大页数
soup = BeautifulSoup(start_html.text,"html.parser")
page = soup.find_all('a',class_='num',rel='nofollow')
max_page = int(page[2].text) +1
same_url = 'http://www.win400