目的: 寂寞的夜晚總需要精神依托,用Python滋潤疲憊的身心!!! 效果: 實現類 1 from Common import Common 2 import requests 3 4 class Get_mn(Common): 5 """ 6 title:Python爬取成人網站圖片Demo 7 ...
目的:
寂寞的夜晚總需要精神依托,用Python滋潤疲憊的身心!!!
效果:
實現類
1 from Common import Common 2 import requests 3 4 class Get_mn(Common): 5 """ 6 title:Python爬取成人網站圖片Demo 7 time:2020/03/08 00:54 8 """ 9 def mb(self): 10 url = 'https://www.449mk.com/pic/5/2020-01-10/25477.html' 11 #創建打開Chrome()對象,訪問url 12 self.open_url(url) 13 #獲取圖片標題 14 img_title = self.get_text("xpath",'/html/body/div[8]/div/div[1]') 15 #獲取圖片列表 16 img_list = self.locateElement("xpath_s",'/html/body/div[8]/div/div[3]/p[*]/img') 17 for index, item in enumerate(img_list): 18 # 獲取列表src屬性中的href屬性 19 item_href = item.get_attribute("src") 20 # 導入訪問圖片的src 21 res = requests.get(item_href) 22 # 定義生成圖片的名稱 23 img_name = img_title+"[" + str(index+1) + "].jpg" 24 # 生成圖片 25 print(img_name+"開始寫入-------") 26 with open('./image/'+img_name, 'wb') as f: 27 f.write(res.content) 28 print(img_name+"第"+str(index+1)+"張圖片下載完成") 29 30 if __name__ == '__main__': 31 mn = Get_mn() 32 mn.mb()
公共類:
1 from selenium import webdriver 2 from time import sleep 3 4 class Common(object): 5 def __init__(self): 6 self.chrome = webdriver.Chrome() 7 self.chrome.implicitly_wait(2) 8 self.chrome.maximize_window() 9 10 def open_url(self,url): 11 self.chrome.get(url) 12 self.chrome.implicitly_wait(10) 13 14 def locateElement(self,locate_type,value): 15 le = None 16 if locate_type == 'css': 17 el = self.chrome.find_element_by_css_selector(value) 18 elif locate_type == 'name': 19 el = self.chrome.find_element_by_name(value) 20 elif locate_type == 'class': 21 el = self.chrome.find_element_by_class_name(value) 22 elif locate_type == 'class_s': 23 el = self.chrome.find_elements_by_class_name(value) 24 elif locate_type == 'id': 25 el = self.chrome.find_element_by_id(value) 26 elif locate_type == 'tag': 27 el = self.chrome.find_element_by_tag_name(value) 28 elif locate_type == 'tag_s': 29 el = self.chrome.find_elements_by_tag_name(value) 30 elif locate_type == 'xpath': 31 el = self.chrome.find_element_by_xpath(value) 32 elif locate_type == 'xpath_s': 33 el = self.chrome.find_elements_by_xpath(value) 34 elif locate_type == 'text': 35 el = self.chrome.find_element_by_link_text(value) 36 elif locate_type == 'text_s': 37 el = self.chrome.find_elements_by_link_text(value) 38 elif locate_type == 'liketext': 39 el = self.chrome.find_element_by_partial_link_text(value) 40 elif locate_type == 'liketext_s': 41 el = self.chrome.find_elements_by_partial_link_text(value) 42 43 if el is not None: 44 return el 45 46 def click(self,locate_type,value): 47 el = self.locateElement(locate_type,value) 48 el.click() 49 50 def input_data(self,locate_type,value,date): 51 el = self.locateElement(locate_type,value) 52 el.send_keys(date) 53 54 def get_text(self,locate_type,value): 55 el = self.locateElement(locate_type,value) 56 return el.text 57 58 def get_attr(self,locate_type,value,attrname): 59 el = self.locateElement(locate_type,value) 60 return el.get_attribute(attrname) 61 62 63 def close_win(self): 64 self.chrome.quit() 65 66 def __del__(self): 67 sleep(1) 68 self.chrome.close()