單線程爬取彼岸桌面美女壁紙

爬蟲未加入多線程,有興趣研究的可以深入了解
其實(shí)用Scrapy爬取效率更高,代碼更少.這個腳本需要第三方庫requests和BeautifulSoup4

腳本未做優(yōu)化或試錯

捕獲.PNG
import requests
from bs4 import BeautifulSoup
import os

url = 'http://www.netbian.com/meinv/'
path = './美女壁紙/'


def get_html(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}
    try:
        res = requests.get(url, headers=headers)
        if res.ok:
            res.encoding = res.apparent_encoding
            return res.text
        return None
    except:
        print('訪問出錯!!')


def get_img_url(url=url):
    html = get_html(url)
    if html:
        soup = BeautifulSoup(html, 'html.parser')
        links = soup.select("div.list b a[href*=.htm]")
        for link in links:
            url = link.get('href').replace('.htm', '-1920x1080.htm').replace('/desk', 'http://www.netbian.com/desk')
            get_wallpage(url)
        prev = soup.select('div.page > a.prev')
        for a in prev:
            if "下一頁" in a.text:
                next_page = 'http://www.netbian.com' + a.get('href')
                print('獲取下一頁' + next_page)
        get_img_url(next_page)


def get_wallpage(url):
    html = get_html(url)
    if html:
        soup = BeautifulSoup(html, 'html.parser')
        if soup.select("img[title]"):
            link = soup.select("img[title]")[0]
            title = link.get('title')
            src = link.get('src')
            save_wallpage(title, src)


def save_wallpage(name, src):
    global path
    image = requests.get(src)
    if image.ok:
        print('正在保存...' + name)
        with open(path + name + '.jpg', 'wb') as f:
            f.write(image.content)


get_img_url()


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 爬蟲文章 in 簡書程序員專題: like:128-Python 爬取落網(wǎng)音樂 like:127-【圖文詳解】py...
    喜歡吃栗子閱讀 22,682評論 4 411
  • 爬蟲文章 in 簡書程序員專題: like:128 - Python 爬取落網(wǎng)音樂 like:127 - 【圖文詳...
    treelake閱讀 29,744評論 33 638
  • 當(dāng)你站在了一個高度,俯視腳下的萬事萬物,不管你背后有不有更高的山峰,你只顧享受眼前的一覽眾山小的虛榮,...
    烽火煤閱讀 147評論 0 0
  • 透明的氣泡 如何在這孤獨(dú)的城市落下 安居 我忘記了 大地是不能觸碰的 一夜細(xì)雨 無處可躲的 我知道 我早就知...
    趙晨君閱讀 196評論 0 0
  • 今天發(fā)現(xiàn)一件事情。 我曾經(jīng)以為,那些在心里被反復(fù)斟酌了很久的話終于說出口應(yīng)該會很痛快,可是說出口的那一剎那卻發(fā)現(xiàn)自...
    彩云聚散閱讀 353評論 0 0

友情鏈接更多精彩內(nèi)容