爬虫-东北林业大学校内-中国[哈尔滨]森林博物馆-爬取所有馆藏蝴蝶图片
馆藏蝴蝶图片爬虫
由于本人很喜欢玩LOL大嘴的帝王斑蝶皮肤,导员下午又发了森林博物馆开馆的通知,于是想前往观赏帝王斑蝶,但是登录预约系统发现,下午并没有开馆,周四周五周六才开馆。所以就去官网看看蝴蝶的图片,由于官网并没有检索功能,所以就想把图片爬下来,再检索一下帝王斑蝶。
直接上代码
import requests
from lxml import etree
import urllib3
if __name__ == "__main__":
# 这里的urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)用来忽略警告
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
url_first = "https://cfm.nefu.edu.cn/zpjc/kczp.htm"
url_main = 'https://cfm.nefu.edu.cn'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.37'
}
pic_url_list = []
for i in range(1,31):
url = ''
if i==30 :
url = url_first
else :
url = "https://cfm.nefu.edu.cn/zpjc/kczp"+'/'+str(i)+".htm"
page_text = requests.get(url=url,headers=headers,verify=False).text
tree = etree.HTML(page_text)
li_list = tree.xpath('//div[@class="pic_list"]/ul/li')
for li in li_list:
pic_src = li.xpath('./a/@href')[0]
pic_name = li.xpath('./a/@title')[0]
s_pic_name = pic_name.encode('raw_unicode_escape')
ss_pic_name = s_pic_name.decode()
pic_url_list.append((ss_pic_name,(url_main+pic_src)))
for pic in pic_url_list:
pic_path = 'museum_pic_data/' + pic[0] + ".jpg"
pic_data = requests.get(url=pic[1], headers=headers, verify=False).content
with open(pic_path, 'wb') as fp:
fp.write(pic_data)
print(pic[0], '下载完成!')