想看美女图片?请用Python实现!,


目的:

  寂寞的夜晚总需要精神依托,用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()

 

  

相关内容

    暂无相关文章

评论关闭