爬蟲

import re
import urllib.request

start_url = 'http://49.232.208.237/zs/'

def get_source(url):
    """
    獲取網(wǎng)頁源代碼。
    :param url: 網(wǎng)址
    :return: 網(wǎng)頁源代碼
    """
    html = urllib.request.urlopen(url)          # 獲取URL頁面源代碼
    return html.read().decode('UTF-8')          # 網(wǎng)站編碼


def getPageList(html):
    """
    獲取頁碼列表和頁碼鏈接
    :param html: 頁面源代碼
    :return: 每頁鏈接和頁標(biāo)題
    """
    pageListLink = []
    toc_block = re.findall('<div>(.*?)</div>', html, re.S)[0]   # 提取所有頁列表
    pageTitle = re.findall('.html">(.*?)</a>', html, re.S)      # 提取頁標(biāo)題
    urls = re.findall('href="(.*?)"', toc_block, re.S)          # 提取頁面鏈接
    for url in urls:
        pageListLink.append(start_url + url)                    # 組合鏈接存入列表
    return pageTitle, pageListLink


def getJobInto(html):
    """
    或缺每頁的信息列表
    :param html: 頁面源代碼
    :return: 列表信息
    """
    jobs = []
    JobInto = re.findall('<div data-v-6c88a070="" class="jobP-info">(.*?)</a></section>', html, re.S)    # 匹配所有工作信息
    for job in JobInto:         # 遍歷每一個工作,提取工作的詳細(xì)信息
        into=[]                 # 提取工作的信息存入此列表
        title = re.search('class="job-name fl">(.*?)</div>', job, re.S).group(1)    # 提取標(biāo)題
        salary = re.search('class="fl">(.*?)</div>', job, re.S).group(1)            # 提取薪資
        city = re.search('class="ads">(.*?)</span>', job, re.S).group(1)            # 提取城市
        company = re.search('class="comp-name fl">(.*?)</div>', job, re.S).group(1) # 提取公司
        time = re.search('class="time fr">(.*?)</div>', job, re.S).group(1)         # 提取時間
        into.append(title)          # 將每個信息加入到列表
        into.append(salary)
        into.append(city)
        into.append(company)
        into.append(time)
        jobs.append(into)           # 將整理出來的工作信息加入到j(luò)obs列表
    return jobs                     # 每個工作的信息都以列表形式存入到j(luò)obs列表里,返回該jobs列表


def save(allJob, title):
    """
    將每一章保存到本地。
    :param allJob: 工作列表
    :param title: 頁面標(biāo)題
    :return: None
    """
    with open('c:\\Users\\ShacoZ\\Desktop\\' + title + '.txt', 'w', encoding='utf-8') as f:
        for jobs in allJob:                     # 遍歷所有工作信息,寫入到文件
            f.write("標(biāo)題:"+jobs[0]+'\n')
            f.write("薪水:"+jobs[1]+'\n')
            f.write("城市:"+jobs[2]+'\n')
            f.write("公司:"+jobs[3]+'\n')
            f.write("發(fā)布時間:"+jobs[4]+'\n')
            f.write('--------------------------------------------------------------------')
            f.write('\n')

if __name__ == '__main__':
    page = get_source(start_url)                # 獲取主頁源代碼
    title, pages = getPageList(page)            # 獲取頁面標(biāo)題和頁面鏈接
    for (job,tit) in zip(pages,title):          # 遍歷鏈接和標(biāo)題
        alljob = getJobInto(get_source(job))    # 將鏈接的頁面源代碼傳給獲取工作頁面的函數(shù),并返回所有工作列表
        save(alljob, tit)                       # 將整理出來的工作列表傳給寫入文件函數(shù)


import re
import urllib.request

start_url = 'http://49.232.208.237/zd/'

def get_source(url):
    """
    獲取網(wǎng)頁源代碼。
    :param url: 網(wǎng)址
    :return: 網(wǎng)頁源代碼
    """
    html = urllib.request.urlopen(url)          # 獲取URL頁面源代碼
    return html.read().decode('UTF-8')          # 編碼網(wǎng)頁


def getPageList(html):
    """
    獲取頁碼列表和頁碼鏈接
    :param html: 目錄頁源代碼
    :return: 每頁鏈接
    """
    pageNumberLink = []
    pageLink = re.findall('class="listhref" href="(.*?)">', html, re.S)   # 提取所有頁面鏈接
    pageTitle = re.findall('.html">(.*?)</a>', html, re.S)                # 頁碼文字
    for url in pageLink:
        pageNumberLink.append(start_url + url)                            # 將鏈接組合
    return pageTitle, pageNumberLink                                      # 返回鏈接列表和頁面標(biāo)題


def getBlogInto(html):
    """
    或缺每頁的信息列表
    :param html: 正文源代碼
    :return: 列表信息
    """
    blogs = []

    blogInto = re.findall('item project-item">(.*?)</div>\s*</div>', html, re.S)    # 獲取頁面所有文章列表
    for blog in blogInto:               # 遍歷文章列表,提取文章信息
        into=[]
        if re.search('<span class="project-name">(.*?)</span>', blog, re.S):      # 判斷是否能匹配到,匹配不到則設(shè)置為空串
            protect = re.search('<span class="project-name">(.*?)</span>', blog, re.S).group(1)     # 匹配到項目名信息(標(biāo)題1)
        else:
            protect = ''
        if re.search('project-title">(.*?)</span>', blog, re.S):
            title = re.search('project-title">(.*?)</span>', blog, re.S).group(1)           # 標(biāo)題2
        else:
            title = ''
        if re.search('data-tooltip="國產(chǎn)">(.*?)</div>', blog, re.S):
            domestic = re.search('data-tooltip="國產(chǎn)">(.*?)</div>', blog, re.S).group(1)      # 是否是國產(chǎn)
        else:
            domestic = ''
        if re.search('line-clamp">(.*?)</p>', blog, re.S):
            text = re.search('line-clamp">(.*?)</p>', blog, re.S).group(1)              # 匹配正文
        else:
            text = ''
        if re.search('<div class="item">(.*?)</div>', blog, re.S):
            collection = re.search('<div class="item">(.*?)</div>', blog, re.S).group(1)    # 收藏數(shù)
        else:
            collection = ''
        if re.search('target="_blank">(.*?)</a>', blog, re.S):
            comment = re.search('target="_blank">(.*?)</a>', blog, re.S).group(1)           # 評論
        else:
            comment = ''
        if re.search('更新于 (.*)',blogInto[8],re.S):
            time = re.search('更新于 (.*)',blogInto[8],re.S).group(1)              # 更新時間
        else:
            time = ''

        into.append(protect)            # 將匹配的信息加入到信息列表
        into.append(title)
        into.append(domestic)
        into.append(text)
        into.append(collection)
        into.append(comment)
        into.append(time)
        blogs.append(into)          # 將單個文章信息列表加入到總列表里

    return blogs                    # 返回所有文章信息


def save(Blogs, title):
    """
    將每一章保存到本地。
    :param Blogs: 文章列表
    :param title: 頁碼標(biāo)題
    :return: None
    """
    with open('c:\\Users\\ShacoZ\\Desktop\\' + title + '.txt', 'w', encoding='utf-8') as f:
        for Blog in Blogs:
            f.write("標(biāo)題:"+Blog[0])
            f.write(Blog[1]+'\n')
            if Blog[2]!='':                         # 如果存在國產(chǎn)字段則寫入國產(chǎn),否則不寫入
                f.write("國產(chǎn):"+Blog[2]+'\n')
            f.write("正文:"+Blog[3]+'\n')
            f.write(Blog[4]+'\n')
            f.write(Blog[5] + '\n')
            f.write("更新時間:" + Blog[6] + '\n')
            f.write('--------------------------------------------------------------------')
            f.write('\n')

if __name__ == '__main__':
    page = get_source(start_url)                # 獲取主鏈接源代碼
    title, pages = getPageList(page)            # 匹配頁碼列表和列表鏈接
    for (job,tit) in zip(pages,title):
        allBlog = getBlogInto(get_source(job))  # 遍歷鏈接和頁碼標(biāo)題,提取每頁的所有文章信息
        save(allBlog, tit)                      #  將文章信息列表傳給寫入文件函數(shù)


?著作權(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)容

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