• <tbody id="baor6"></tbody>

  • <em id="baor6"><acronym id="baor6"><input id="baor6"></input></acronym></em>
    <strike id="baor6"></strike><li id="baor6"></li>
    <rp id="baor6"></rp>
    1. <em id="baor6"></em>
      <form id="baor6"></form>
      <nobr id="baor6"></nobr>

      <em id="baor6"><acronym id="baor6"><u id="baor6"></u></acronym></em>

          葵花寶典教程,一個自學編程平臺

          葵花寶典教程,一個自學編程平臺

          sudo go 提示找不到命令:sudo: go: command not found

          環境變量配置:

          # ~/.bash_profile

          export GOROOT=/usr/local/go

          export GOPATH=/usr/local/GO

          export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

          錯誤描述:

          使用非root用戶,執行 go version 獲得正確輸出。

          使用root用戶,執行 go verison 獲得正確輸出。

          使用非root用戶,執行 sudo go version 獲得輸出 sudo: go: command not found。

          解決辦法:

          根據 Some programs not found when used with sudo 中的回答。


          我們使用 sudo 時,使用的配置文件是 /etc/sudoers。


          配置 /etc/sudoers 文件中的 Defaults secure_path 這一項。將 $GOROOT/bin 目錄加入進去。(請使用絕對目錄,如:/usr/local/go/bin/)


          Defaults secure_path 這一行:

          Defaults    secure_path = /usr/local/go/bin/:/sbin:/bin:/usr/sbin:/usr/bin

          go mod tidy 報錯:XXXX found,but does not contain package XXXX

          舉個例子:

          gitee.com/atix/utils/logger: module gitee.com/atix/utils@latest found (v0.1.0), but does not contain package gitee.com/atix/utils/logger

          來看上文這個報錯,由于我們 import 時未指定特定版本號,故 golang 默認使用最新版本(即能夠通過 git checkout 將其檢出的版本號),我們去查看該 module,發現有一個 tag 號為:v0.1.0,檢查該 tag 代碼發現確實沒有我們要用的 logger 包,由此判斷是我們 import 了一個尚未發布的 package,碰到這種情況只需要將該 module 重新打個 tag,將我們用到的 package 發布,然后本地執行:

          go get gitee.com/atix/utils@v0.1.1 把新的版本下載、安裝到本地,之后就正常了。

          go get  github.com/aaa/lotus@latest    [最新版]

          go面試題

          Go學習-make和new的區別

          https://blog.csdn.net/ouyangyiwen/article/details/111548053

          [

          new和make的區別,我們從下圖便可看出:

                   1)new是為值類型分配內存(可以任意類型的數據),其返回的是指針,指向分配類型的內存地址。         

                   2)make為引用類型分配內存并初始化,如:chan、map和slice,其返回值為這個類型(引用)本身。   

                   3)new 分配的空間被清零。make 分配空間后,會進行初始化;

          ]

          golang切片和數組的區別


          數組是值拷貝而切片是引用

          https://www.cnblogs.com/blog-196/p/11141356.html

          Go語言切片詳解 

          https://www.cnblogs.com/lvnux/p/12907356.html


          goland使用go mod模式

          使用go mod之后,想要在goland中有代碼提示,有兩種方式,一種是使用gopath下的goimport工具,另一種是使用gomod自身的管理工具

          我是用的是非gopath的方式,每次新建項目后總是報錯

          go list -m: can‘t compute ‘all‘ using the vendor directory
          (Use -mod=mod or -mod=readonly to bypass.),

          得不到想要的效果,最后終于發現是步驟不對

          第一步:創建空文件夾

          第二步:goland以項目方式打開文件夾

          第三步:設置goland中的配置,goroot,gomodule


          1-1.png

          第四步:執行go mod init + 項目名,這個截圖的地方多一個go modules,用于存放下載的包的


          2-2.png

          第五步:創建.go文件,然后寫上代碼

          第六步:執行go mod tidy,下載所需的包,也會刪除多余的包


          Laravel DB::table update record

          I have this query

           DB::table('product_options')
                  ->where('product_id', $id)
                  ->where('size', $selected_size)
                  ->update(array('stock' => WHAT TO PUT HERE));

          In the update part where I've put WHAT TO UPDATE, what should I put here to decrease the number by 1?

          DB::table('product_options')
                  ->where('product_id', $id)
                  ->where('size', $selected_size)
                  ->decrement('stock');


          當 Target 類 [Controller] 不存在時怎么辦. 出現在 Laravel

          在最近的 Laravel(可能是 v8 或更高版本)中,如果你在初始狀態下在 routes/web.php 中編寫以下代碼,你將無法找到應該在那里的 Controller Class,并且會出現錯誤。

          Route::get('/hoge', 'Controller@index');
          Illuminate\Contracts\Container\BindingResolutionException Target class [Controller] does not exist.  http://laravel.internal/hoge  Illuminate\Container\Container::build htdocs\laravel\vendor\laravel\framework\src\Illuminate\Container\Container.php:811

          顯然,我開始忘記了 Contoller 的初始位置。

          一致

          $namespace取消注釋App / Providers / RouteServiceProvider.php

              /**      
              * The controller namespace for the application.      *      
              * When present, controller route declarations will automatically be prefixed with this namespace.           
              * @var string|null      
              */     
              protected $namespace = 'App\\Http\\Controllers'; //


          python字符串連接的幾種方法

          python字符串連接的方法,一般有以下三種:

          方法1:直接通過加號(+)操作符連接

          1
          website = 'python' + 'tab' + '.com'

          方法2:join方法

          1
          2
          listStr = ['python''tab''.com'
          website = ''.join(listStr)

          方法3:替換

          1
          website = '%s%s%s' % ('python''tab''.com')

          下面再來說一下三種方法的不同

          方法1,使用簡單直接,但是網上不少人說這種方法效率低

          之所以說python 中使用 + 進行字符串連接的操作效率低下,是因為python中字符串是不可變的類型,使用 + 連接兩個字符串時會生成一個新的字符串,生成新的字符串就需要重新申請內存,當連續相加的字符串很多時(a+b+c+d+e+f+...) ,效率低下就是必然的了

           

          方法2,使用略復雜,但對多個字符進行連接時效率高,只會有一次內存的申請。而且如果是對list的字符進行連接的時候,這種方法必須是首選

           

          方法3:字符串格式化,這種方法非常常用,本人也推薦使用該方法


          Python爬蟲常用庫總結之“Requests”

          Requests模塊介紹:

          發送http請求,獲取響應數據

          requests模塊是一個第三方模塊,需要在你的python(虛擬)環境中額外安裝: 

          pip/pip3 install requests

          requests基礎:

          requests模塊發送get請求


          • import requests

          • # 目標url

          • url = 'https://www.baidu.com'

          • # 向目標url發送get請求

          • response = requests.get(url)

          • # 打印響應內容

          • print(response.text)


          Response響應對象:

          觀察上邊代碼運行結果發現,有好多亂碼;這是因為編解碼使用的字符集不同造成的;我們嘗試使用下邊的辦法來解決中文亂碼問題


          • import requests

          • url = 'https://www.baidu.com'

          • # 向目標url發送get請求

          • response = requests.get(url)

          • # 打印響應內容

          • # print(response.text)

          • print(response.content.decode()) # 注意這里!


          1、response.text是requests模塊按照chardet模塊推測出的編碼字符集進行解碼的結果

          2、網絡傳輸的字符串都是bytes類型的,所以response.text = response.content.decode(‘推測出的編碼字符集’)

          3、我們可以在網頁源碼中搜索charset,嘗試參考該編碼字符集,注意存在不準確的情況。


          Response.text 和Response.content的區別:

          1、Response.text


          • 類型:str

          • 解碼類型: requests模塊自動根據HTTP 頭部對響應的編碼作出有根據的推測,推測的文本編碼


          2、Response.content


          • 類型:bytes

          • 解碼類型: 沒有指定



          解決中文亂碼:

          通過對response.content進行decode,來解決中文亂碼


          1、Response.content.decode() 默認utf-8

          2、Response.content.decode("GBK")

          3、常見的編碼字符集

          • utf-8

          • gbk

          • gb2312

          • ascii (讀音:阿斯克碼)

          • iso-8859-1



          Response響應對象的其它常用屬性或方法:


          • #https://beishan.blog.csdn.net/

          • # 1.2.3-response其它常用屬性

          • import requests


          • # 目標url

          • url = 'https://www.baidu.com'


          • # 向目標url發送get請求

          • response = requests.get(url)


          • # 打印響應內容

          • # print(response.text)

          • # print(response.content.decode()) # 注意這里!

          • print(response.url)# 打印響應的url

          • print(response.status_code)# 打印響應的狀態碼

          • print(response.request.headers)# 打印響應對象的請求頭

          • print(response.headers)# 打印響應頭

          • print(response.request._cookies)# 打印請求攜帶的cookies

          • print(response.cookies)# 打印響應中攜帶的cookies



          Requests實操:

          requests模塊發送請求

          發送帶header的請求

          我們先寫一個獲取百度首頁的代碼



          • import requests

          • url = 'http://www.voyageurimaginaire.com'

          • response = requests.get(url)

          • print(response.content.decode())

          • # 打印響應對應請求的請求頭信息

          • print(response.request.headers)


          從瀏覽器中復制User-Agent,構造Headers字典;完成下面的代碼后,運行代碼查看結果


          • import requests


          • url = 'http://www.voyageurimaginaire.com'


          • headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}


          • # 在請求頭中帶上User-Agent,模擬瀏覽器發送請求

          • response = requests.get(url, headers=headers)


          • print(response.content)


          • # 打印請求頭信息

          • print(response.request.headers)


          發送帶參數的請求:

          我們在使用百度搜索的時候經常發現url地址中會有一個 ?,那么該問號后邊的就是請求參數,又叫做查詢字符串


          在url攜帶參數,直接對含有參數的url發起請求


          • import requests


          • headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}


          • url = 'https://www.baidu.com/s?wd=python'


          • response = requests.get(url, headers=headers)



          通過Params攜帶參數字典:

          1.構建請求參數字典

          2.向接口發送請求的時候帶上參數字典,參數字典設置給params


          • import requests


          • headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}


          • # 這是目標url

          • # url = 'https://www.baidu.com/s?wd=python'


          • # 最后有沒有問號結果都一樣

          • url = 'https://www.baidu.com/s?'


          • # 請求參數是一個字典 即wd=python

          • kw = {'wd': 'python'}


          • # 帶上請求參數發起請求,獲取響應

          • response = requests.get(url, headers=headers, params=kw)


          • print(response.content)

          • 從瀏覽器中復制User-Agent和Cookie

          • 瀏覽器中的請求頭字段和值與headers參數中必須一致

          • headers請求參數字典中的Cookie鍵對應的值是字符串

          • import requests


          • url = 'https://github.com/USER_NAME'


          • # 構造請求頭字典

          • headers = {

          • # 從瀏覽器中復制過來的User-Agent

          • 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',

          • # 從瀏覽器中復制過來的Cookie

          • 'Cookie': 'xxx這里是復制過來的cookie字符串'

          • }


          • # 請求頭參數字典中攜帶cookie字符串

          • resp = requests.get(url, headers=headers)


          • print(resp.text)




          超時參數timeout的使用:

          在平時網上沖浪的過程中,我們經常會遇到網絡波動,這個時候,一個請求等了很久可能仍然沒有結果。

          在爬蟲中,一個請求很久沒有結果,就會讓整個項目的效率變得非常低,這個時候我們就需要對請求進行強制要求,讓他必須在特定的時間內返回結果,否則就報錯。


          1、超時參數timeout的使用方法

          response = requests.get(url, timeout=3)

          2、timeout=3表示:發送請求后,3秒鐘內返回響應,否則就拋出異常


          • import requests



          • url = 'https://twitter.com'

          • response = requests.get(url, timeout=3) # 設置超時時間



          Requests發送post請求的方法:


          • response = requests.post(url, data)

          • data參數接收一個字典

          • requests模塊發送post請求函數的其它參數和發送get請求的參數完全一致


          注意運行的時候開梯子就會報錯



          Python 爬蟲利器三之 Xpath 語法與用法

          安裝

          1
          pip install lxml

          初步使用

          首先我們利用它來解析 HTML 代碼,先來一個小例子來感受一下它的基本用法。

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          from lxml import etree
          text = '''
          <div>
             <ul>
                  <li class="item-0"><a href="link1.html">first item</a></li>
                  <li class="item-1"><a href="link2.html">second item</a></li>
                  <li class="item-inactive"><a href="link3.html">third item</a></li>
                  <li class="item-1"><a href="link4.html">fourth item</a></li>
                  <li class="item-0"><a href="link5.html">fifth item</a>
              </ul>
          </div>
          '''
          html = etree.HTML(text)
          result = etree.tostring(html)
          print(result)

          首先我們使用 lxml 的 etree 庫,然后利用 etree.HTML 初始化,然后我們將其打印出來。 其中,這里體現了 lxml 的一個非常實用的功能就是自動修正 html 代碼,大家應該注意到了,最后一個 li 標簽,其實我把尾標簽刪掉了,是不閉合的。不過,lxml 因為繼承了 libxml2 的特性,具有自動修正 HTML 代碼的功能。 所以輸出結果是這樣的

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          <html><body>
          <div>
             <ul>
                  <li class="item-0"><a href="link1.html">first item</a></li>
                  <li class="item-1"><a href="link2.html">second item</a></li>
                  <li class="item-inactive"><a href="link3.html">third item</a></li>
                  <li class="item-1"><a href="link4.html">fourth item</a></li>
                  <li class="item-0"><a href="link5.html">fifth item</a></li>
          </ul>
          </div>

          </body></html>

          不僅補全了 li 標簽,還添加了 body,html 標簽。

          文件讀取

          除了直接讀取字符串,還支持從文件讀取內容。比如我們新建一個文件叫做 hello.html,內容為

          1
          2
          3
          4
          5
          6
          7
          8
          9
          <div>
             <ul>
                  <li class="item-0"><a href="link1.html">first item</a></li>
                  <li class="item-1"><a href="link2.html">second item</a></li>
                  <li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li>
                  <li class="item-1"><a href="link4.html">fourth item</a></li>
                  <li class="item-0"><a href="link5.html">fifth item</a></li>
              </ul>
          </div>

          利用 parse 方法來讀取文件。

          1
          2
          3
          4
          from lxml import etree
          html = etree.parse('hello.html')
          result = etree.tostring(html, pretty_print=True)
          print(result)

          同樣可以得到相同的結果。

          XPath 實例測試

          依然以上一段程序為例 (1)獲取所有的

          =

          [ 取li text

          result = html.xpath('/html/body/div[1]/div/div[1]/ul[1]/li[3]')

          print (result[0].xpath('string(.)').strip())

          ]

          =

          [取a text

          result = html.xpath('/html/body/div[1]/div/div[1]/ul[1]/li[3]/a/text()')

          print (result)

          ]

          =

          [取a  href

          result = html.xpath('/html/body/div[1]/div/div[1]/ul[1]/li[3]/a/@href')

          result = html.xpath('/html/body/div[1]/div/div[1]/ul[1]/li[3]/a/@href')


          print (result)

          ]

          文件自動加https

          <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

          <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

          <meta name="viewport" content="width=device-width,initial-scale=1.0">

          <meta http-equiv="Cache-control" content="public">

          <meta property="og:site_name" content="Trioangle" />

          <meta property="og:image" content="/images/logo.svg" />

          <meta property="og:image:secure_url" content="/images/logo.svg" />

          <meta property="og:locale" content="en_US" />

          <meta property="og:type" content="article" />


          << 1 2 3 > >>

          Copyright www.voyageurimaginaire.com Rights Reserved葵花寶典教程.鄂icp2022001145號-1

          分享:

          支付寶

          微信

          精品一区二区三区无码毛片
        1. <tbody id="baor6"></tbody>

        2. <em id="baor6"><acronym id="baor6"><input id="baor6"></input></acronym></em>
          <strike id="baor6"></strike><li id="baor6"></li>
          <rp id="baor6"></rp>
          1. <em id="baor6"></em>
            <form id="baor6"></form>
            <nobr id="baor6"></nobr>

            <em id="baor6"><acronym id="baor6"><u id="baor6"></u></acronym></em>