|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求各位鱼油帮帮忙,代码如下:
- import requests
- from bs4 import BeautifulSoup
- from multiprocessing import Pool
- # 获取页面url列表
- def get_list(url,headers):
- try:
- res = requests.get(url,headers = headers)
- if res.status_code == 200:
- soup = BeautifulSoup(res.content,'html.parser')
- lis = soup.find('div',class_='tab_box').find_all('li')
- for i in lis:
- li = i.find('a').get('href')
- into_url(li,headers)
- except Exception as e:
- print('获取链接失败:%s'%e)
- # 进入页面
- def into_url(url,headers):
- try:
- res = requests.get(url,headers = headers)
- if res.status_code == 200:
- soup = BeautifulSoup(res.content,'html.parser')
- imgurls = soup.find('ul',class_='scroll-img clearfix').find_all('li')
- for i in imgurls:
- imgurl = i.find('a').get('href')
- response = requests.get(imgurl,headers = headers)
- soups = BeautifulSoup(response.content,'html.parser')
- downimg = soups.find('img',class_='pic-large').get('src')
- title = soups.find('div',class_='ptitle').text
- download(downimg,title,headers)
- except Exception as e:
- print('进入图册失败:%s'%e)
- # 下载函数
- def download(url,title,headers):
- print('获取图片链接成功:%s'%url)
- try:
- img = requests.get(url,headers = headers).content
- with open(title + '.jpg','wb') as f:
- f.write(img)
- except Exception as e:
- print('下载失败:%s'%e)
- #主函数
- def main(num):
- headers = {'accept-encoding' : 'gzip, deflate, br','accept-language' : 'zh-CN,zh;q=0.9','cookie' : '__cfduid=d785b1a05ce079fc4b377ba844ad7db101551847363; PHPSESSID=doob179fokrompnom6d574fod7','user-agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}
- url = 'http://www.win4000.com/zt/meinv_' + str(num) + '.html'
- get_list(url,headers)
-
- if __name__ == '__main__':
- pool = Pool()
- pool.map(main,[i for i in range(1,6)])
复制代码 |
|