美文网首页
无聊的爬虫 爬取煎蛋网的妹子图

无聊的爬虫 爬取煎蛋网的妹子图

作者: Daoba | 来源:发表于2018-07-08 21:11 被阅读0次

           周末无聊没事做写了个小爬虫
           爬虫爬取的页面通常有两种一种是不存在动态加载数据的页面,另外一种是存在动态加载页面的网页,对于第一种爬取比较简单,一般得到html的源码解析到相应的内容即可,而对于动态加载数据页面的爬取则要分析网页提交的数据,对网站进行逆向爬虫工程对于没有网络基础的人来说还是有一定的难度,另外一种傻瓜方法就是通过火狐或者谷歌提供的浏览器驱动模拟加载数据得到最终的页面源码也能爬取到相应的数据缺点是速度比较慢。
           今天就介绍一个简单的方法爬取煎蛋妹子图,下载chromedriver.exe用于模拟加载动态数据。下面上代码

    #!/usr/bin/python
    #coding=utf-8
    #date 2018.07.08
    import urllib
    from bs4 import BeautifulSoup
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.chrome.options import Options
    # 通过webdriver得到动态页面的html源码
    def getPage(url):
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        driver = webdriver.Chrome(chrome_options=chrome_options)
        driver.get(url)
        return driver.page_source
    # 下载保存图片
    def downLoadimg(html):
        sup = BeautifulSoup(html,'html.parser')
        for imgurl in sup.find_all(class_='view_img_link'):
            img = 'http:'+ imgurl['href']
            # print(img)
            filename = img.split('/')[-1]
            print(img.split('/')[-1])
            urllib.urlretrieve(img, 'D:/meizi/' + filename)
    
    if __name__=="__mian__":
        base_url = "https://jandan.net/ooxx/page-"
        baseurl = "#comments"
        # 定义下载页数
        for i in range(50,0,-1):
            url = base_url+str(i)+baseurl
            print(url)
            try:
                html = getPage(url)
            except TimeoutException as t:
                print("File download:"+str(i))
            downLoadimg(html)
    

    --headless无头模式,在写本文的时已经不支持PHantomjs了如果使用会报错。
    BeautifulSoup解析html自己下去脑补。

    由于图片劲爆自己下码爬取

    相关文章

      网友评论

          本文标题:无聊的爬虫 爬取煎蛋网的妹子图

          本文链接:https://www.haomeiwen.com/subject/pzxauftx.html