import time
from concurrent.futures import ThreadPoolExecutor
import time
import os
import re
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
rootrurl = 'https://tu.fengniao.com'
save_dir = 'D:/estimages/'
chromeExeLoc = 'D:/software/chrome/chromedriver_win32/chromedriver.exe'
headers = {
"Referer": rootrurl,
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
'Accept-Language': 'en-US,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive'
} ###设置请求的头部,伪装成浏览器
def saveOneImg(dir, img_url):
new_headers = {
"Referer": img_url,
'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
'Accept-Language': 'en-US,en;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive'
} ###设置请求的头部,伪装成浏览器,实时换成新的 header 是为了防止403 http code问题,防止反盗链,
try:
img = requests.get(img_url, headers=new_headers) # 请求图片的实际URL
if (str(img).find('200') > 1):
with open(
'{}/{}.jpg'.format(dir, img_url.split('/')[-1]), 'wb') as jpg: # 请求图片并写进去到本地文件
jpg.write(img.content)
print(img_url)
jpg.close()
return True
else:
return False
except Exception as e:
print('exception occurs: ' + img_url)
print(e)
return False
def find_all(sub, s):
index_list = []
index = s.find(sub)
while index != -1:
index_list.append(index)
index = s.find(sub, index + 1)
if len(index_list) > 0:
return index_list
else:
return -1
def downPics(dir, url):
# 得到所与图片的链接
html = BeautifulSoup(requests.get(url).text, features="html.parser")
html = str(html)
a = html[html.find("var picList = eval") : html.find("var picListNum = picList.length")]
idx1 = find_all('https', a)
idx2 = find_all('jpg', a)
# 图片去重与,url整理组装
bb = set()
for i in range(len(idx1)):
img_url = a[idx1[i] : idx2[i]+3]
img_url = img_url.replace('\\', '')
bb.add(img_url)
# 逐个下载啊
for img in bb:
saveOneImg(dir, img)
def getSubTitleName(str):
cop = re.compile("[^\u4e00-\u9fa5^a-z^A-Z^0-9]") # 匹配不是中文、大小写、数字的其他字符
string1 = cop.sub('', str) # 将string1中匹配到的字符替换成空字符
return string1
def tagSpider(tag, url):
# 无头浏览器 这样浏览器就不会弹出那个chrome的web浏览器界面
options = Options()
options.add_argument('--headless')
browser = webdriver.Chrome(chromeExeLoc, options=options)
browser.maximize_window()
browser.implicitly_wait(5)
browser.get(url)
js = "window.scrollTo(0,document.body.scrollHeight)" # 定义鼠标滑倒底部的动作
pics = None
while 1:
browser.execute_script(js) # 每次往下滑到底部,直到有了200个组图就推出了
pics = browser.find_elements_by_class_name('picA')
print('current length is %d:', len(pics))
if len(pics) >= 200:
break
time.sleep(5)
# 开始爬取图片
for li in pics:
# 创建目录
subDir = getSubTitleName(li.find_element_by_class_name('pic').get_attribute('title'))
new_dir = '{}{}/{}'.format(save_dir, tag, subDir)
if not os.path.exists(new_dir):
os.makedirs(new_dir)
# 下载组图
downPics(new_dir, li.get_attribute('href')) # 爬取每个图片组
def getAllTags():
list = {}
html = BeautifulSoup(requests.get(rootrurl).text, features="html.parser")
a_s = html.find('div', {'class' : 'labelMenu module90'}).find_all('a')
for a in a_s:
list[a.get_text()] = rootrurl + a.get('href')
return list
if __name__ == '__main__':
# 获得所有标签
taglist = getAllTags()
print(taglist)
#
# 给每个标签配备一个线程
# with ThreadPoolExecutor(max_workers=15) as t: # 创建一个最大容纳数量为20的线程池
# for tag, url in taglist.items():
# t.submit(tagSpider, tag, url)
# 单个连接测试下下
tagSpider('美女', 'https://tu.fengniao.com/13/')
# 等待所有线程都完成。
while 1:
print('-------------------')
time.sleep(1)