虛擬環(huán)境

為什么需要虛擬環(huán)境:

到目前位置,我們所有的第三方包安裝都是直接通過(guò)pip install xx的方式進(jìn)行安裝的,這樣安裝會(huì)將那個(gè)包安裝到你的系統(tǒng)級(jí)的Python環(huán)境中。但是這樣有一個(gè)問(wèn)題,就是如果你現(xiàn)在用Django 1.10.x寫了個(gè)網(wǎng)站,然后你的領(lǐng)導(dǎo)跟你說(shuō),之前有一個(gè)舊項(xiàng)目是用Django 0.9開發(fā)的,讓你來(lái)維護(hù),但是Django 1.10不再兼容Django 0.9的一些語(yǔ)法了。這時(shí)候就會(huì)碰到一個(gè)問(wèn)題,我如何在我的電腦中同時(shí)擁有Django 1.10Django 0.9兩套環(huán)境呢?這時(shí)候我們就可以通過(guò)虛擬環(huán)境來(lái)解決這個(gè)問(wèn)題。

虛擬環(huán)境原理介紹:

虛擬環(huán)境相當(dāng)于一個(gè)抽屜,在這個(gè)抽屜中安裝的任何軟件包都不會(huì)影響到其他抽屜。并且在項(xiàng)目中,我可以指定這個(gè)項(xiàng)目的虛擬環(huán)境來(lái)配合我的項(xiàng)目。比如我們現(xiàn)在有一個(gè)項(xiàng)目是基于Django 1.10.x版本,又有一個(gè)項(xiàng)目是基于Django 0.9.x的版本,那么這時(shí)候就可以創(chuàng)建兩個(gè)虛擬環(huán)境,在這兩個(gè)虛擬環(huán)境中分別安裝Django 1.10.xDjango 0.9.x來(lái)適配我們的項(xiàng)目。

安裝virtualenv

virtualenv是用來(lái)創(chuàng)建虛擬環(huán)境的軟件工具,我們可以通過(guò)pip或者pip3來(lái)安裝:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n9" mdtype="fences" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;"> pip install virtualenv
pip3 install virtualenv</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n12" mdtype="fences" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;"> virtualenv [虛擬環(huán)境的名字]</pre>

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n25" mdtype="fences" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;"> virtualenv -p C:\Python36\python.exe [virutalenv name]</pre>

  1. 創(chuàng)建虛擬環(huán)境:

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">mkvirtualenv my_env</pre>

    那么會(huì)在你當(dāng)前用戶下創(chuàng)建一個(gè)Env的文件夾,然后將這個(gè)虛擬環(huán)境安裝到這個(gè)目錄下。 如果你電腦中安裝了python2python3,并且兩個(gè)版本中都安裝了virtualenvwrapper,那么將會(huì)使用環(huán)境變量中第一個(gè)出現(xiàn)的Python版本來(lái)作為這個(gè)虛擬環(huán)境的Python解釋器。

  2. 切換到某個(gè)虛擬環(huán)境:

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">workon my_env</pre>

  3. 退出當(dāng)前虛擬環(huán)境:

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">deactivate</pre>

  4. 刪除某個(gè)虛擬環(huán)境:

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n49" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">rmvirtualenv my_env</pre>

  5. 列出所有虛擬環(huán)境:

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n52" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">lsvirtualenv</pre>

  6. 進(jìn)入到虛擬環(huán)境所在的目錄:

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n55" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">cdvirtualenv</pre>

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n60" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;"> mkvirtualenv --python==C:\Python36\python.exe qf_env</pre>

    這是一個(gè)庫(kù) python用來(lái)操作url的模塊 https://docs.python.org/2.7/library/urllib2.html

    • urllib2 python2.7自帶 不需要安裝 導(dǎo)入即可
    • urllib2 在 python3中升級(jí)到了 urllib.request

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="python" cid="n267" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;"># -- coding:utf-8 --
    import urllib2

    url = 'http://www.baidu.com/'

    urlopen需要三個(gè)參數(shù)

    url 你要抓取的url

    data 默認(rèn)為None 為none說(shuō)明get 請(qǐng)求 如果你data不為none 就認(rèn)為是post請(qǐng)求

    timeout 超時(shí)時(shí)間

    res = urllib2.urlopen(url, data=None)

    print res

    print res.read() #返回所有的內(nèi)容

    print res.readline() #按照行返回

    print res.readlines() #返回所有的行

    print res.getcode() #獲取客戶端請(qǐng)求的狀態(tài)

    print res.geturl() #獲取請(qǐng)求的url

    print res.code

    print res.read().decode('utf-8')

    encode</pre>

    第一個(gè)反反爬

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="python" cid="n270" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">User-Agent 偽造請(qǐng)求頭 http://www.jsons.cn/useragent/ User-Agent在線解析

    -- coding:utf-8 --

    import urllib2

    headers = {
    "User-Agent":"User-Agent, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
    }

    url后邊要加上/

    url = "http://www.baidu.com/"

    創(chuàng)建請(qǐng)求對(duì)象

    res = urllib2.Request(url,headers=headers)

    print res.get_full_url() #獲取完整的url
    print res.get_method() #獲取請(qǐng)求方法
    print res.get_header("User-agent") #獲取 瀏覽器的名稱
    print res.get_host() #host名稱
    print res.get_type() #協(xié)議名稱
    res.add_header("Connection","keep-alive") #wang header頭中添加請(qǐng)求信息

    print res.get_header("Connection")
    </pre>

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="python" cid="n272" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">模擬百度搜索

    -- coding:utf-8 --

    import os
    import urllib
    import urllib2

    urllib 和 urllib2 的區(qū)別:

    urllib僅僅接收url 能用urlencode 進(jìn)行編碼 urllib.urlencode()

    urllib2 可以接收設(shè)置了 headers 的 Request類

    以上 就讓我們經(jīng)常兩個(gè)搭配來(lái)使用

    https://www.baidu.com/s?wd=美女

    def baidu_search(params):

    headers = {
        "User-Agent":"User-Agent, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
    }
    
    url = "http://www.baidu.com/s?"+params
    
    requests = urllib2.Request(url,headers=headers) #創(chuàng)建請(qǐng)求對(duì)象
    #print requests
    response = urllib2.urlopen(requests)  #把請(qǐng)求對(duì)象傳給 requests
    #print response
    print response.read()  #讀取內(nèi)容  
    
    dir = './'
    os.chdir(dir)
    file = urllib2.urlopen(url).read()
    open('baidu.html',"wb").write(file)   #保存頁(yè)面  
    print "OK"
    

    if name == "main":
    kw = raw_input("請(qǐng)輸入要查找的內(nèi)容")
    params = {
    'wd':kw #拼接參數(shù)
    }
    params = urllib.urlencode(params) #將字典傳承字符串參數(shù)
    baidu_search(params)

    </pre>

    python3 爬蟲

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n275" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">import urllib
    from urllib import request

    headers = {
    "User-Agent":"User-Agent, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
    }

    url

    url = "http://www.baidu.com/"

    創(chuàng)建請(qǐng)求對(duì)象

    req =urllib.request.Request(url,headers=headers)

    發(fā)送請(qǐng)求 獲取響應(yīng)

    responses = urllib.request.urlopen(req)

    print(responses)#二進(jìn)制

    print(""30)

    print(responses.read())

    print(""30)

    print(responses.read().decode('utf-8'))#解碼

    字符串->字節(jié) encode

    字節(jié)-> 字符串 decode </pre>

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n277" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">python3模擬百度搜索
    from urllib import request
    import urllib.parse
    headers = {
    "User-Agent":"User-Agent, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
    }

    /s?wd=

    kw = input("請(qǐng)輸入關(guān)鍵字:")
    params = {
    'wd':kw
    }

    將字典解析成參數(shù)字符串

    params=urllib.parse.urlencode(params)
    print(params)

    創(chuàng)建url

    url = 'http://www.baidu.com/s?'+params

    創(chuàng)建請(qǐng)求對(duì)象

    requests= urllib.request.Request(url,headers=headers)
    responses = urllib.request.urlopen(requests)

    print(responses.read().decode('utf-8'))

    print(responses.status)

    print(responses.dict)
    </pre>

    python3 urllib 下載

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n280" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">import urllib.request

    第一個(gè)參數(shù) 要下載的url

    第二個(gè)參數(shù) 文件存放的路徑

    request.urlretrieve("http://www.so.com/",r"baidu.html")

    http://www.baidu.com/?username=中文&password=afdsf

    url中只能寫 -_.a-z 如果中文需要轉(zhuǎn)義 urllib.parse.quote 解碼 urllib.parse.unquote

    urllib.request.urlretrieve("http://p1.so.qhimgs1.com/bdr/576__/t01ff3c9aedf92d7281.jpg",r"蒼老師.jpg")

    strings = 'http://www.baidu.com/?username=中文&password=afdsf'

    print(urllib.parse.quote(strings))

    strings = 'http%3A//www.baidu.com/%3Fusername%3D%E4%B8%AD%E6%96%87%26password%3Dafdsf'
    print(urllib.parse.unquote(strings))</pre>

    抓取前程無(wú)憂

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n283" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">import re
    import urllib.request

    headers = {
    "User-Agent":"User-Agent, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
    }

    獲取前程無(wú)憂的接口

    url = "https://search.51job.com/list/040000%252C010000,000000,0000,00,9,99,python,2,1.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare="

    抓取數(shù)據(jù) 創(chuàng)建請(qǐng)求對(duì)象

    req = urllib.request.Request(url,headers=headers)

    獲取服務(wù)器響應(yīng)數(shù)據(jù)

    response = urllib.request.urlopen(req)

    print(response)

    解碼

    html = response.read().decode('gbk')

    print(html)

    print(type(html))

    處理數(shù)據(jù) 拿到標(biāo)簽中間所有的內(nèi)容

    jobnum_re = '<div class="rt">(.*?)</div>'
    coms = re.compile(jobnum_re,re.S)
    strs = coms.findall(html)[0]

    print(strs)

    貪婪模式 非貪婪模式

    非貪婪模式加上 ? 變成了 貪婪模式

    取出 純數(shù)字

    num_re = '.?(\d+).'
    num = re.findall(num_re,strs)

    print(num)

    print(int(num[0]))

    獲取第一個(gè)崗位信息

    jobname_re = '<div class="el">(.*?)</div>'
    joblist = re.findall(jobname_re,html,re.S)

    #print(joblist[0]) #這是第一個(gè)崗位的信息 多個(gè)標(biāo)簽

    #匹配崗位內(nèi)容

    jobnameone_re = 'onmousedown="">(.*?)</a>'

    jobnameone_list = re.findall(jobnameone_re,joblist[1],re.S)

    print(jobnameone_list[0].strip())

    for job in joblist:
    jobnameone_re = 'onmousedown="">(.*?)</a>'
    jobnameone_list = re.findall(jobnameone_re, job, re.S)
    print(jobnameone_list)
    #print("崗位名稱:",jobnameone_list[0].strip())

    </pre>

    網(wǎng)易云音樂(lè)

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n286" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">import json
    import urllib.request
    import urllib.parse

    headers = {
    "User-Agent":"User-Agent, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
    }
    url = 'https://music.163.com/weapi/v1/resource/comments/A_PL_0_2386637465?csrf_token='
    data = {
    "params": "IMMh0aWzreEhrbqaIjYKCkSvKIarMpJsMTcQ92b4lDOnlOZ6bNlkE/rAPDWDjIIOcdLHuLvuWk6tOvAe8r0JU93bUs1peT2w5Zc1zoiUVTAXXJaJOI9ZUp02VLLA/aqPh3ugCeE9T/oXYMknnASO25t8lhFcRpM3TAPArQsYhYqBCp0lBsifqnxktlMfN8jhBwXXxJzq2v2dtXIb9K7oMLeySbFgz4Rx+sC3olkP4fQ=",
    "encSecKey": "0afc0ae61575480fd9bc168ef82df0d0ed2c699657f91000baf019f14600d19882d1334bb2f5703c658f6300c57935bd8278183765a5aeaa773c25673b24a125584cefee6d30cebc6fb3f40891a8e047b1626e091dfaf0279de90104af64eec23411f560d14278fe69b28bb292a883b58747ae5a03357ea243d3832f1c64b9a6"
    }

    將post請(qǐng)求傳入二進(jìn)制參數(shù)

    data = urllib.parse.urlencode(data).encode()

    print(data)

    創(chuàng)建請(qǐng)求對(duì)象 加上data 就變成了post請(qǐng)求

    req = urllib.request.Request(url,headers=headers,data=data)

    獲取返回?cái)?shù)據(jù)

    response = urllib.request.urlopen(req)
    content = response.read().decode()
    print(content)

    comments_list = json.loads(content)
    host_comments = comments_list['hotComments']
    for contents in host_comments:
    nickname = contents['user']['nickname']
    content = contents['content']
    print(nickname,":",content)

    </pre>

    豆瓣

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n289" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">import json
    import urllib.request

    headers = {
    "User-Agent":"User-Agent, Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11"
    }
    for i in range(100):
    url = "https://movie.douban.com/j/new_search_subjects?sort=T&range=0,10&tags=&start=%d"%(im
    requests = urllib.request.Request(url,headers=headers)
    responses = urllib.request.urlopen(requests)
    content = responses.read().decode()
    #print(content) #json 數(shù)據(jù)

    data = json.loads(content)
    print(data)
    data_list = data.get('data')
    for movie in data_list:
        title = movie['title']
        casts = movie['casts']
        print("電影名:%s 主演:%s" %(title,casts))
    

    </pre>

    作業(yè) 抓取 阿里招聘

    https://job.alibaba.com/zhaopin/positionList.htm 抓取前10頁(yè) 并寫入頁(yè)面

    學(xué)歷、部門、崗位要求、工作經(jīng)驗(yàn)

    urllib 庫(kù)

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n255" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">請(qǐng)求 (Request) 部分詳解
    1. Headers —— 顯示客戶端發(fā)送到服務(wù)器的 HTTP 請(qǐng)求的 header, 顯示為一個(gè)分級(jí)視圖,包含了 Web 客戶端信息、Cookie、傳輸狀 態(tài)等。
    2. Textview——顯示POST請(qǐng)求的body部分為文本。
    3. WebForms——顯示請(qǐng)求的GET參數(shù)和POSTbody內(nèi)容。
    4. HexView——用十六進(jìn)制數(shù)據(jù)顯示請(qǐng)求。
    5. Auth——顯示響應(yīng)header中的Proxy-Authorization(代理身份驗(yàn)
    證) 和 Authorization(授權(quán)) 信息.
    6. Raw——將整個(gè)請(qǐng)求顯示為純文本。
    7. JSON-顯示JSON格式文件。
    8. XML——如果請(qǐng)求的body是XML格式,就是用分級(jí)的XML樹來(lái)
    顯示它。

    響應(yīng) (Response) 部分詳解

    1. Transformer——顯示響應(yīng)的編碼信息。
    2. Headers——用分級(jí)視圖顯示響應(yīng)的header。
    3. TextView——使用文本顯示相應(yīng)的body。
    4. ImageVies——如果請(qǐng)求是圖片資源,顯示響應(yīng)的圖片。
    5. HexView——用十六進(jìn)制數(shù)據(jù)顯示響應(yīng)。
    6. WebView——響應(yīng)在Web瀏覽器中的預(yù)覽效果。
    7. Auth——顯示響應(yīng)header中的Proxy-Authorization(代理身份驗(yàn)
    證) 和 Authorization(授權(quán)) 信息。
    8. Caching——顯示此請(qǐng)求的緩存信息。
    9. Privacy——顯示此請(qǐng)求的私密(P3P)信息。
    10. Raw —— 將整個(gè)響應(yīng)顯示為純文本。
    11. JSON-顯示JSON格式文件。
    12. XML —— 如果響應(yīng)的 body 是 XML 格式,就是用分級(jí)的 XML 樹
    來(lái)顯示它 。
    </pre>

    fidder

    Chrome瀏覽器是一個(gè)非常親近開發(fā)者的瀏覽器??梢苑奖愕牟榭淳W(wǎng)絡(luò)請(qǐng)求以及發(fā)送的參數(shù)。對(duì)著網(wǎng)頁(yè)右鍵->檢查。然后就可以打開開發(fā)者選項(xiàng)。

    Chrome抓包工具:

    503:服務(wù)出錯(cuò) 由于臨時(shí)的服務(wù)器維護(hù)或者過(guò)載,服務(wù)器當(dāng)前無(wú)法處理請(qǐng)求。這個(gè)狀況是臨時(shí)的,并且將在一段時(shí)間以后恢復(fù)。

    502:錯(cuò)誤網(wǎng)關(guān) 作為網(wǎng)關(guān)或者代理工作的服務(wù)器嘗試執(zhí)行請(qǐng)求時(shí),從上游服務(wù)器接收到無(wú)效的響應(yīng)。

    501:服務(wù)器無(wú)法識(shí)別 服務(wù)器不支持當(dāng)前請(qǐng)求所需要的某個(gè)功能。當(dāng)服務(wù)器無(wú)法識(shí)別請(qǐng)求的方法,并且無(wú)法支持其對(duì)任何資源的請(qǐng)求。

    500:服務(wù)器內(nèi)部錯(cuò)誤??赡苁欠?wù)器出現(xiàn)bug了。 重點(diǎn)

    404:沒(méi)有找到 處理方式:丟棄重點(diǎn)

    403:服務(wù)器拒絕訪問(wèn),權(quán)限不夠。重點(diǎn)

    401:未授權(quán) 處理方式:丟棄

    400:請(qǐng)求的url在服務(wù)器上找不到。換句話說(shuō)就是請(qǐng)求url錯(cuò)誤。重點(diǎn)

    304:請(qǐng)求的資源未更新 處理方式:丟棄,使用本地緩存文件

    302:臨時(shí)重定向。比如在訪問(wèn)一個(gè)需要登錄的頁(yè)面的時(shí)候,而此時(shí)沒(méi)有登錄,那么就會(huì)重定向到登錄頁(yè)面。 重點(diǎn)

    300:該狀態(tài)碼不被HTTP/1.0的應(yīng)用程序直接使用, 只是作為3XX類型回應(yīng)的默認(rèn)解釋。存在多個(gè)可用的被請(qǐng)求資源。 處理方式:若程序中能夠處理,則進(jìn)行進(jìn)一步處理,如果程序中不能處理,則丟棄 301:永久重定向。比如在訪問(wèn)www.360buy.com的時(shí)候會(huì)重定向到www.jd.com。 重點(diǎn)

    204:服務(wù)器端已經(jīng)實(shí)現(xiàn)了請(qǐng)求,但是沒(méi)有返回新的信 息。如果客戶是用戶代理,則無(wú)須為此更新自身的文檔視圖。 處理方式:丟棄

    202:請(qǐng)求被接受,但處理尚未完成 處理方式:阻塞等待

    201:請(qǐng)求完成,結(jié)果是創(chuàng)建了新資源。新創(chuàng)建資源的URI可在響應(yīng)的實(shí)體中得到 處理方式:爬蟲中不會(huì)遇到

    200:請(qǐng)求成功 處理方式:獲得響應(yīng)的內(nèi)容,進(jìn)行處理. 重點(diǎn)

    102:繼續(xù)處理 由WebDAV(RFC 2518)擴(kuò)展的狀態(tài)碼,代表處理將被繼續(xù)執(zhí)行。

    101: 轉(zhuǎn)換協(xié)議 在發(fā)送完這個(gè)響應(yīng)最后的空行后,服務(wù)器將會(huì)切換到在Upgrade 消息頭中定義的那些協(xié)議。只有在切換新的協(xié)議更有好處的時(shí)候才應(yīng)該采取類似措施。

    100:繼續(xù) 客戶端應(yīng)當(dāng)繼續(xù)發(fā)送請(qǐng)求。客戶端應(yīng)當(dāng)繼續(xù)發(fā)送請(qǐng)求的剩余部分,或者如果請(qǐng)求已經(jīng)完成,忽略這個(gè)響應(yīng)。

    常見的http狀態(tài)碼

    常見響應(yīng)狀態(tài)碼:

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n229" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;">常?的請(qǐng)求報(bào)頭

    1. Host
      Host:對(duì)應(yīng)?址 URL 中的 Web 名稱和端?號(hào),?于指定被請(qǐng)求資源的
      Internet 主機(jī)和端?號(hào),它通常從 HTTP URL 中提取出來(lái)的

    2. Connection
      Connection:表示客戶端與服務(wù)連接類型
      1.Client 發(fā)起?個(gè)包含 Connection:keep-alive 的請(qǐng)求,HTTP/1.1 使?keep-alive為默認(rèn)值。
      2.Server 收到請(qǐng)求后:
      如果 Server ?持 keep-alive,回復(fù)?個(gè)包含 Connection:keep-alive的響應(yīng),不關(guān)閉連接;
      如果 Server 不?持 keep-alive,回復(fù)?個(gè)包含 Connection:close 的響應(yīng),關(guān)閉連接。
      3.如果 client 收到包含 Connection:keep-alive 的響應(yīng),向同?個(gè)連接發(fā)送下?個(gè)請(qǐng)求,直到??主動(dòng)關(guān)閉連接。keep-alive 在很多情況下能夠重?連接,減少資源消耗,縮短響應(yīng)時(shí)間,?如當(dāng)瀏覽器需要多個(gè)?件時(shí)(?如?個(gè) HTML?件和相關(guān)的圖形?件),不需要每次都去請(qǐng)求建?連接。

    3.Upgrade-Insecure-Requests
    Upgrade-Insecure-Requests:升級(jí)不安全的請(qǐng)求,意思是會(huì)在加載 http 資 源時(shí)?動(dòng)替換成 https 請(qǐng)求,讓瀏覽器不再顯示 https??中的 http 請(qǐng)求警報(bào)。
    HTTPS 是以安全為?標(biāo)的 HTTP 通道,所以在 HTTPS 承載的??上不允許出現(xiàn) HTTP 請(qǐng)求,?旦出現(xiàn)就是提示或報(bào)錯(cuò),但是很多?站對(duì) https 沒(méi)有技術(shù)概念,在填?的數(shù)據(jù)中不免出現(xiàn) http 的資源

    4.User-Agent User-Agent:是客戶瀏覽器的名稱,以后會(huì)詳細(xì)講。

    5.Accept
    Accept:指瀏覽器或其他客戶端可以接受的 MIME(Multipurpose Internet Mail Extensions(多? 途互聯(lián)?郵件擴(kuò)展))?件類型,服務(wù)器可以根據(jù)它判斷并返回適當(dāng)?shù)?件格式。
    舉例:
    Accept: / :表示什么都可以接收。
    Accept:image/gif :表明客戶端希望接受 GIF 圖像格式的資源;
    Accept:text/html :表明客戶端希望接受 html?本。
    Accept: text/html, application/xhtml+xml;q=0.9, image/*;q=0.8 :表示
    瀏覽器?持的 MIME 類型分別是 html?本、xhtml 和 xml?檔、所有的圖像格式資源。
    q 是權(quán)重系數(shù),范圍 0 =< q <= 1,q 值越?,請(qǐng)求越傾向于獲得其“;”之前的類型表示的內(nèi)容。若沒(méi)有指 定 q 值,則默認(rèn)為 1,按從左到右排序順序;若被 賦值為 0,則?于表示瀏覽器不接受此內(nèi)容類型。
    Text:?于標(biāo)準(zhǔn)化地表示的?本信息,?本消息可以是多種字符集和或者多種格式的;Application:?于傳 輸應(yīng)?程序數(shù)據(jù)或者?進(jìn)制數(shù)據(jù)。詳細(xì)請(qǐng)點(diǎn)擊

    6.Referer
    Referer:表明產(chǎn)?請(qǐng)求的??來(lái)?于哪個(gè) URL,?戶是從該 Referer??訪問(wèn)到當(dāng)前請(qǐng)求的??。這個(gè)屬 性可以?來(lái)跟蹤 Web 請(qǐng)求來(lái)?哪個(gè)??,是從 什么?站來(lái)的等。

    7.Accept-Encoding
    Accept-Encoding:指出瀏覽器可以接受的編碼?式。編碼?式不同于?件格式,它是為了壓縮?件并加速? 件傳遞速度。瀏覽器在接收到 Web 響應(yīng)之后先解碼,然后再檢查?件格式,許多情形下這可以減少?量的下 載時(shí)間。
    舉例:Accept-Encoding:gzip;q=1.0, identity; q=0.5, *;q=0
    如果有多個(gè) Encoding 同時(shí)匹配, 按照 q 值順序排列,本例中按順序?持 gzip,
    identity 壓縮編碼,?持 gzip 的瀏覽器會(huì)返回經(jīng)過(guò) gzip 編碼的 HTML??。如 果請(qǐng)求消息中沒(méi)有設(shè)置 這個(gè)域服務(wù)器假定客戶端對(duì)各種內(nèi)容編碼都可以接受。

    8.Accept-Language
    Accept-Langeuage:指出瀏覽器可以接受的語(yǔ)?種類,如 en 或 en-us 指英語(yǔ),zh 或者 zh-cn 指中 ?,當(dāng)服務(wù)器能夠提供?種以上的語(yǔ)?版本時(shí)要?到。

    9.Accept-Charset
    Accept-Charset:指出瀏覽器可以接受的字符編碼。
    舉例:Accept-Charset:iso-8859-1,gb2312,utf-8
    ISO8859-1:通常叫做 Latin-1。Latin-1 包括了書寫所有??歐洲語(yǔ)?不可缺少的附加字符,英?瀏覽器的默認(rèn)值是 ISO-8859-1. gb2312:標(biāo)準(zhǔn)簡(jiǎn)體中?字符集;
    utf-8:UNICODE 的?種變?字符編碼,可以解決多種語(yǔ)??本顯示問(wèn)題,從?實(shí)現(xiàn)應(yīng)?國(guó)際化和本地化。
    如果在請(qǐng)求消息中沒(méi)有設(shè)置這個(gè)域,缺省是任何字符集都可以接受。

    10.Cookie
    Cookie:瀏覽器?這個(gè)屬性向服務(wù)器發(fā)送 Cookie。Cookie 是在瀏覽器中寄存 的?型數(shù)據(jù)體,它可以記載和服 務(wù)器相關(guān)的?戶信息,也可以?來(lái)實(shí)現(xiàn)會(huì)話 功能。

    11.Content-Type
    Content-Type:POST 請(qǐng)求??來(lái)表示的內(nèi)容類型。
    舉例:Content-Type = Text/XML; charset=gb2312:
    指明該請(qǐng)求的消息體中包含的是純?本的 XML 類型的數(shù)據(jù),字符編碼采?“gb2312”。

    服務(wù)端 HTTP響應(yīng) HTTP 響應(yīng)也由四個(gè)部分組成,分別是: 狀態(tài)? 、 消息報(bào)頭 、 正? 、 響 應(yīng)

    常?的響應(yīng)報(bào)頭(了解)
    理論上所有的響應(yīng)頭信息都應(yīng)該是回應(yīng)請(qǐng)求頭的。但是服務(wù)端為了效率,安全,還有其他??的考慮,會(huì)添加相對(duì)應(yīng)的響應(yīng)頭信息,從上圖可以看到:

    1. Cache-Control:must-revalidate, no-cache,
      private。

      這個(gè)值告訴客戶端,服務(wù)端不希望客戶端緩存資源,在下次請(qǐng)求資源時(shí),必須要從新請(qǐng)求服務(wù)器,不能從緩存副本中獲取資源。
      Cache-Control 是響應(yīng)頭中很重要的信息,當(dāng)客戶端請(qǐng)求頭中包含 Cache- Control:max-age=0 請(qǐng)求,明確表示不會(huì)緩存服務(wù)器資源時(shí),Cache-
      Control 作為作為回應(yīng)信息,通常會(huì)返回 no-cache,意思就是說(shuō),"那就不 緩存唄"。
      當(dāng)客戶端在請(qǐng)求頭中沒(méi)有包含 Cache-Control 時(shí),服務(wù)端往往會(huì)定,不同的資源不同的緩存策略,?如說(shuō) oschina 在緩存圖?資源的策略就是Cache-Control:max-age=86400,這個(gè)意思是,從當(dāng)前時(shí)間開始,在
      86400 秒的時(shí)間內(nèi),客戶端可以直接從緩存副本中讀取資源,?不需要向服務(wù)器請(qǐng)求。

    2. Connection:keep-alive
      這個(gè)字段作為回應(yīng)客戶端的 Connection:keep-alive,告訴客戶端服務(wù)器的
      tcp 連接也是?個(gè)?連接,客戶端可以繼續(xù)使?這個(gè) tcp 連接發(fā)送 http 請(qǐng)求。

    3. Content-Encoding:gzip
      告訴客戶端,服務(wù)端發(fā)送的資源是采?gzip 編碼的,客戶端看到這個(gè)信息后,應(yīng)該采?gzip 對(duì)資源進(jìn)?解碼。

    4. Content-Type:text/html;charset=UTF-8
      告訴客戶端,資源?件的類型,還有字符編碼,客戶端通過(guò) utf-8 對(duì)資源進(jìn)
      ? 解碼,然后對(duì)資源進(jìn)?html 解析。通常我們會(huì)看到有些?站是亂碼的, 往往 就是服務(wù)器端沒(méi)有返回正確的編碼。
      5. Date:Sun, 21 Sep 2014 06:18:21 GMT

    這個(gè)是服務(wù)端發(fā)送資源時(shí)的服務(wù)器時(shí)間,GMT 是格林尼治所在地的標(biāo)準(zhǔn)時(shí)間。http 協(xié)議中發(fā)送的時(shí)間都是 GMT 的,這主要是解決在互聯(lián)?上,不同時(shí)區(qū)在相互請(qǐng)求資源的時(shí)候,時(shí)間混亂問(wèn)題。

    1. Expires:Sun, 1 Jan 2000 01:00:00 GMT
      這個(gè)響應(yīng)頭也是跟緩存有關(guān)的,告訴客戶端在這個(gè)時(shí)間前,可以直接訪問(wèn)緩存副本,很顯然這個(gè)值會(huì)存在問(wèn)題,因?yàn)榭蛻舳撕头?wù)器的時(shí)間不?定會(huì)都是相同的,如果時(shí)間不同就會(huì)導(dǎo)致問(wèn)題。所以這個(gè)響應(yīng)頭是沒(méi)有 Cache- Control:max-age=*這個(gè)響應(yīng)頭準(zhǔn)確的,因?yàn)?max-age=date 中的 date 是個(gè)相 對(duì)時(shí)間,不僅更好理解,也更準(zhǔn)確。

    2. Pragma:no-cache 這 個(gè)含義與 Cache-Control 等同。

    3. Server:Tengine/1.4.6
      這個(gè)是服務(wù)器和相對(duì)應(yīng)的版本,只是告訴客戶端服務(wù)器的信息。

    4. Transfer-Encoding:chunked
      這個(gè)響應(yīng)頭告訴客戶端,服務(wù)器發(fā)送的資源的?式是分塊發(fā)送的。?般分塊發(fā)送的資源都是服務(wù)器動(dòng)態(tài)?成的,在發(fā)送時(shí)還不知道發(fā)送資源的??,所以采?分塊發(fā)送,每?塊都是獨(dú)?的,獨(dú)?的塊都能標(biāo)示??的?度,最后
      ?塊是 0?度的,當(dāng)客戶端讀到這個(gè) 0?度的塊時(shí),就可以確定資源已經(jīng)傳輸完了。

    5. Vary: Accept-Encoding
      告訴緩存服務(wù)器,緩存壓縮?件和?壓縮?件兩個(gè)版本,現(xiàn)在這個(gè)字段?處并不?,因?yàn)楝F(xiàn)在的瀏覽器都是?持壓縮的。
      </pre>

    6. User-Agent:瀏覽器名稱。這個(gè)在網(wǎng)絡(luò)爬蟲中經(jīng)常會(huì)被使用到。請(qǐng)求一個(gè)網(wǎng)頁(yè)的時(shí)候,服務(wù)器通過(guò)這個(gè)參數(shù)就可以知道這個(gè)請(qǐng)求是由哪種瀏覽器發(fā)送的。如果我們是通過(guò)爬蟲發(fā)送請(qǐng)求,那么我們的User-Agent就是Python,這對(duì)于那些有反爬蟲機(jī)制的網(wǎng)站來(lái)說(shuō),可以輕易的判斷你這個(gè)請(qǐng)求是爬蟲。因此我們要經(jīng)常設(shè)置這個(gè)值為一些瀏覽器的值,來(lái)偽裝我們的爬蟲。

    7. Referer:表明當(dāng)前這個(gè)請(qǐng)求是從哪個(gè)url過(guò)來(lái)的。這個(gè)一般也可以用來(lái)做反爬蟲技術(shù)。如果不是從指定頁(yè)面過(guò)來(lái)的,那么就不做相關(guān)的響應(yīng)。

    8. Cookiehttp協(xié)議是無(wú)狀態(tài)的。也就是同一個(gè)人發(fā)送了兩次請(qǐng)求,服務(wù)器沒(méi)有能力知道這兩個(gè)請(qǐng)求是否來(lái)自同一個(gè)人。因此這時(shí)候就用cookie來(lái)做標(biāo)識(shí)。一般如果想要做登錄后才能訪問(wèn)的網(wǎng)站,那么就需要發(fā)送cookie信息了。

    http協(xié)議中,向服務(wù)器發(fā)送一個(gè)請(qǐng)求,數(shù)據(jù)分為三部分,第一個(gè)是把數(shù)據(jù)放在url中,第二個(gè)是把數(shù)據(jù)放在body中(在post請(qǐng)求中),第三個(gè)就是把數(shù)據(jù)放在head中。這里介紹在網(wǎng)絡(luò)爬蟲中經(jīng)常會(huì)用到的一些請(qǐng)求頭參數(shù):

    請(qǐng)求頭常見參數(shù):

    1. get請(qǐng)求:一般情況下,只從服務(wù)器獲取數(shù)據(jù)下來(lái),并不會(huì)對(duì)服務(wù)器資源產(chǎn)生任何影響的時(shí)候會(huì)使用get請(qǐng)求。

    2. post請(qǐng)求:向服務(wù)器發(fā)送數(shù)據(jù)(登錄)、上傳文件等,會(huì)對(duì)服務(wù)器資源產(chǎn)生影響的時(shí)候會(huì)使用post請(qǐng)求。 以上是在網(wǎng)站開發(fā)中常用的兩種方法。并且一般情況下都會(huì)遵循使用的原則。但是有的網(wǎng)站和服務(wù)器為了做反爬蟲機(jī)制,也經(jīng)常會(huì)不按常理出牌,有可能一個(gè)應(yīng)該使用get方法的請(qǐng)求就一定要改成post請(qǐng)求,這個(gè)要視情況而定。

    Http協(xié)議中,定義了八種請(qǐng)求方法。這里介紹兩種常用的請(qǐng)求方法,分別是get請(qǐng)求和post請(qǐng)求。

    get、post、delete 、put 、patch

    常用的請(qǐng)求方法:

    在瀏覽器中請(qǐng)求一個(gè)url,瀏覽器會(huì)對(duì)這個(gè)url進(jìn)行一個(gè)編碼。除英文字母,數(shù)字和部分符號(hào)外,其他的全部使用百分號(hào)+十六進(jìn)制碼值進(jìn)行編碼。

    • scheme:代表的是訪問(wèn)的協(xié)議,一般為http或者https以及ftp等。

    • host:主機(jī)名,域名,比如www.baidu.com

    • port:端口號(hào)。當(dāng)你訪問(wèn)一個(gè)網(wǎng)站的時(shí)候,瀏覽器默認(rèn)使用80端口。

    • path:查找路徑。比如:www.itdecent.cn/trending/now,后面的trending/now就是path。

    • query-string:查詢字符串,比如:www.baidu.com/s?wd=python,后面的wd=python就是查詢字符串。

    • anchor:錨點(diǎn),后臺(tái)一般不用管,前端用來(lái)做頁(yè)面定位的。

    <pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" cid="n197" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: pre-wrap; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); border: 1px solid rgb(231, 234, 237); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; position: relative !important;"> scheme://host:port/path/?query-string=xxx#anchor</pre>

    URLUniform Resource Locator的簡(jiǎn)寫,統(tǒng)一資源定位符。 一個(gè)URL由以下幾部分組成:

    url詳解:

    1. 當(dāng)用戶在瀏覽器的地址欄中輸入一個(gè)URL并按回車鍵之后,瀏覽器會(huì)向HTTP服務(wù)器發(fā)送HTTP請(qǐng)求。HTTP請(qǐng)求主要分為“Get”和“Post”兩種方法。

    2. 當(dāng)我們?cè)跒g覽器輸入U(xiǎn)RL http://www.baidu.com 的時(shí)候,瀏覽器發(fā)送一個(gè)Request請(qǐng)求去獲取 http://www.baidu.com 的html文件,服務(wù)器把Response文件對(duì)象發(fā)送回給瀏覽器。

    3. 瀏覽器分析Response中的 HTML,發(fā)現(xiàn)其中引用了很多其他文件,比如Images文件,CSS文件,JS文件。 瀏覽器會(huì)自動(dòng)再次發(fā)送Request去獲取圖片,CSS文件,或者JS文件。

    4. 當(dāng)所有的文件都下載成功后,網(wǎng)頁(yè)會(huì)根據(jù)HTML語(yǔ)法結(jié)構(gòu),完整的顯示出來(lái)了。

    在瀏覽器中發(fā)送一個(gè)http請(qǐng)求的過(guò)程:

    自定義端口號(hào) :1024以前的 需要root權(quán)限

    0~127 都被系統(tǒng)占用了

    ftp:21 smtp:25 redis:6379

    https 443 scp: 22 mysql:3306 mongodb:27017

    http:80 ssh:22 pop3:110 memcached:11211

    HTTP協(xié)議:全稱是HyperText Transfer Protocol,中文意思是超文本傳輸協(xié)議,是一種發(fā)布和接收HTML頁(yè)面的方法。服務(wù)器端口號(hào)是80端口。 HTTPS協(xié)議:是HTTP協(xié)議的加密版本,在HTTP下加入了SSL層。服務(wù)器端口號(hào)是443端口

    什么是http和https協(xié)議: 基于TCP/IP協(xié)議

    1. 配置允許遠(yuǎn)程連接 tools-》options-》connections 左邊倒數(shù)三個(gè)選中 右邊第一列三個(gè)選中

    5.抓取https tools-》options-》https 選擇左邊前三個(gè) 右邊 actions 添加證書到信任機(jī)構(gòu)

    4.清空會(huì)話 點(diǎn)擊X號(hào) remove all 即可

    3.禁止抓包 file 第一個(gè) 取消對(duì)號(hào)即可

    select json /javascript/image

    select html

    2.左下角黑色框 輸入指令

    下面是響應(yīng) response

    上面是請(qǐng)求 request

    右邊分為上下:

    左邊 會(huì)話窗口

    1.頁(yè)面分為左右

    fiddler簡(jiǎn)單用法:

    http協(xié)議和Chrome抓包工具

    1. Python3.6開發(fā)環(huán)境。

    2. Pycharm 2017 professional版。

    3. 虛擬環(huán)境。virtualenv/virtualenvwrapper

    準(zhǔn)備工具:

    2.用代理

    1.偽造用代理 user-agent

    反反爬蟲:

    4.將get 改為post

    3.動(dòng)態(tài)加載數(shù)據(jù) 替代靜態(tài)頁(yè)面

    2.js壓縮混淆加密

    反爬蟲:(面子、競(jìng)爭(zhēng)對(duì)手) 1.驗(yàn)證碼

    爬蟲:最后贏的人 最后不是頁(yè)面結(jié)構(gòu)復(fù)雜、數(shù)據(jù)量大 而是 反爬蟲人員

    爬蟲 、反爬蟲、反反爬蟲 博弈最后贏家 必定是爬蟲

    5.twisted 異步網(wǎng)絡(luò)框架

    4.構(gòu)建分布式 scrapy+redis

    3.動(dòng)態(tài)模擬瀏覽器的行為 selenium 模擬瀏覽器 加載 js 等 獲取驗(yàn)證碼這些

    2.re\XPATH \beautifulsoup4(提取html、xml的庫(kù))\jsonpath\pyquery (網(wǎng)頁(yè)解析庫(kù)) 用來(lái)解析服務(wù)器的響應(yīng)內(nèi)容

    1.HTML

    爬蟲需要掌握什么?

    django、flask、tornado

    1. PHP:PHP是世界是最好的語(yǔ)言,但他天生不是做這個(gè)的,而且對(duì)多線程、異步支持不是很好,并發(fā)處理能力弱。爬蟲是工具性程序,對(duì)速度和效率要求比較高。

    2. Java:生態(tài)圈很完善,是Python爬蟲最大的競(jìng)爭(zhēng)對(duì)手。但是Java語(yǔ)言本身很笨重,代碼量很大。重構(gòu)成本比較高,任何修改會(huì)導(dǎo)致代碼大量改動(dòng)。爬蟲經(jīng)常要修改采集代碼。

    3. C/C++:運(yùn)行效率是無(wú)敵的。但是學(xué)習(xí)和開發(fā)成本高。寫個(gè)小爬蟲程序可能要大半天時(shí)間。

    4. Python:語(yǔ)法優(yōu)美、代碼簡(jiǎn)潔、開發(fā)效率高、支持的模塊多。相關(guān)的HTTP請(qǐng)求模塊和HTML解析模塊非常豐富。還有Scrapy和Scrapy-redis框架讓我們開發(fā)爬蟲變得異常簡(jiǎn)單。

    為什么用Python寫爬蟲程序:

    1. 通用爬蟲:通用爬蟲是搜索引擎抓取系統(tǒng)(百度、谷歌、搜狗等)的重要組成部分。主要是將互聯(lián)網(wǎng)上的網(wǎng)頁(yè)下載到本地,形成一個(gè)互聯(lián)網(wǎng)內(nèi)容的鏡像備份。

      百度、360、搜狗 抓取網(wǎng)頁(yè) 、數(shù)據(jù)存儲(chǔ)、數(shù)據(jù)處理、提供檢索服務(wù) (提供接口)

      robots 協(xié)議 :可以限制 百度、360等 可以爬什么 不可以爬哪些

      淘寶 不允許百度爬取

      僅僅是一個(gè)協(xié)議 完全可以不遵守

      如何讓百度等爬取你的網(wǎng)站 需要提交robots協(xié)議

      SEO:

      pagerank值 (流量、點(diǎn)擊率)值越高 排名越靠前

      百度、360競(jìng)價(jià)排名

      通用爬蟲的缺點(diǎn):

      1.抓取的數(shù)據(jù)太多了 好多沒(méi)用的

      2.不會(huì)根據(jù)用戶的需求 爬取數(shù)據(jù)

    2. 聚焦爬蟲: 也叫專業(yè)爬蟲 主題爬蟲 是面向特定需求的一種網(wǎng)絡(luò)爬蟲程序,他與通用爬蟲的區(qū)別在于:聚焦爬蟲在實(shí)施網(wǎng)頁(yè)抓取的時(shí)候會(huì)對(duì)內(nèi)容進(jìn)行篩選和處理,盡量保證只抓取與需求相關(guān)的網(wǎng)頁(yè)信息。

      1. 根據(jù)自己的需求 設(shè)計(jì)爬取程序 然后抓取對(duì)應(yīng)的數(shù)據(jù)即可

        通過(guò)聚焦爬蟲如何抓取到數(shù)據(jù)?

      1.每個(gè)網(wǎng)頁(yè)都有一個(gè) 統(tǒng)一資源定位符 URL

      2.網(wǎng)頁(yè)都是有標(biāo)簽 組成

      3.遵循h(huán)ttp、https協(xié)議

    通用爬蟲和聚焦爬蟲:

    4.如果頁(yè)面中還有其它的url 那么我們?cè)賵?zhí)行第二步

    3.提取有用的數(shù)據(jù) 進(jìn)行存儲(chǔ)

    2.通過(guò)http\https 協(xié)議獲取對(duì)應(yīng)的頁(yè)面

    1 .確定爬取的url地址 放到隊(duì)列中

    爬蟲設(shè)計(jì)思路

    1. 項(xiàng)目需要 獲取更多的數(shù)據(jù) 并且進(jìn)行分析 形成分析報(bào)告 提供戰(zhàn)略 分析

    2. 進(jìn)行seo優(yōu)化 搜索引擎喜歡爬經(jīng)常更新的網(wǎng)站 爬取數(shù)據(jù)讓網(wǎng)站內(nèi)容每天更新

    為什么要學(xué)習(xí)爬蟲

    1. 通俗理解:爬蟲是一個(gè)模擬人類請(qǐng)求網(wǎng)站行為的程序??梢宰詣?dòng)請(qǐng)求網(wǎng)頁(yè)、并數(shù)據(jù)抓取下來(lái),然后使用一定的規(guī)則提取有價(jià)值的數(shù)據(jù)。

    2. 專業(yè)介紹:百度百科。

      蜘蛛網(wǎng):

      搜索引擎 蜘蛛

      往:互聯(lián)網(wǎng)

    什么是網(wǎng)絡(luò)爬蟲:

    1. 搜索引擎(百度、谷歌、360搜索等)。

    2. 伯樂(lè)在線。

    3. 惠惠購(gòu)物助手。

    4. 數(shù)據(jù)分析與研究(數(shù)據(jù)冰山知乎專欄)。

    5. 搶票軟件等。

    爬蟲的實(shí)際例子:

    爬蟲前奏

    在使用mkvirtualenv的時(shí)候,可以指定--python的參數(shù)來(lái)指定具體的python路徑:

    創(chuàng)建虛擬環(huán)境的時(shí)候指定Python版本: 重點(diǎn)

    我的電腦->右鍵->屬性->高級(jí)系統(tǒng)設(shè)置->環(huán)境變量->系統(tǒng)變量中添加一個(gè)參數(shù)WORKON_HOME,將這個(gè)參數(shù)的值設(shè)置為你需要的路徑。 重點(diǎn)

    修改mkvirtualenv的默認(rèn)路徑:

virtualenvwrapper基本使用:

  1. *nix:pip install virtualenvwrapper。

  2. windows:pip install virtualenvwrapper-win。

安裝virtualenvwrapper

virtualenvwrapper這個(gè)軟件包可以讓我們管理虛擬環(huán)境變得更加簡(jiǎn)單。不用再跑到某個(gè)目錄下通過(guò)virtualenv來(lái)創(chuàng)建虛擬環(huán)境,并且激活的時(shí)候也要跑到具體的目錄下去激活。

virtualenvwrapper:


在電腦的環(huán)境變量中,一般是不會(huì)去更改一些環(huán)境變量的順序的。也就是說(shuō)比如你的Python2/ScriptsPython3/Scripts的前面,那么你不會(huì)經(jīng)常去更改他們的位置。但是這時(shí)候我確實(shí)是想在創(chuàng)建虛擬環(huán)境的時(shí)候用Python3這個(gè)版本,這時(shí)候可以通過(guò)-p參數(shù)來(lái)指定具體的Python解釋器:

創(chuàng)建虛擬環(huán)境的時(shí)候指定Python解釋器:

退出虛擬環(huán)境很簡(jiǎn)單,通過(guò)一個(gè)命令就可以完成:deactivate。

退出虛擬環(huán)境:

  1. windows進(jìn)入虛擬環(huán)境:進(jìn)入到虛擬環(huán)境的Scripts文件夾中,然后執(zhí)行activate。

  2. *nix進(jìn)入虛擬環(huán)境:source /path/to/virtualenv/bin/activate 一旦你進(jìn)入到了這個(gè)虛擬環(huán)境中,你安裝包,卸載包都是在這個(gè)虛擬環(huán)境中,不會(huì)影響到外面的環(huán)境。

虛擬環(huán)境創(chuàng)建好了以后,那么可以進(jìn)入到這個(gè)虛擬環(huán)境中,然后安裝一些第三方包,進(jìn)入虛擬環(huán)境在不同的操作系統(tǒng)中有不同的方式,一般分為兩種,第一種是Windows,第二種是*nix

進(jìn)入環(huán)境:

如果你當(dāng)前的Python3/Scripts的查找路徑在Python2/Scripts的前面,那么將會(huì)使用python3作為這個(gè)虛擬環(huán)境的解釋器。如果python2/Scriptspython3/Scripts前面,那么將會(huì)使用Python2來(lái)作為這個(gè)虛擬環(huán)境的解釋器。

創(chuàng)建虛擬環(huán)境非常簡(jiǎn)單,通過(guò)以下命令就可以創(chuàng)建了:

創(chuàng)建虛擬環(huán)境:

port)

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

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

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