<ruby id="dzldf"></ruby>

<span id="dzldf"><th id="dzldf"></th></span>
<em id="dzldf"><form id="dzldf"></form></em>
<address id="dzldf"></address>

<form id="dzldf"><th id="dzldf"><progress id="dzldf"></progress></th></form>

    <form id="dzldf"></form>
    <em id="dzldf"><address id="dzldf"></address></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.bstarfood.com'

    • response = requests.get(url)

    • print(response.content.decode())

    • # 打印响应对应请求的请求头信息

    • print(response.request.headers)


    从浏览器中复制User-Agent,构造Headers字典;完成下面的代码后,运行代码查看结果


    • import requests


    • url = 'http://www.bstarfood.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.bstarfood.com Rights Reserved葵花宝典教程.鄂icp2022001145号-1

    分享:

    支付宝

    微信

    找妓女网站
    <ruby id="dzldf"></ruby>

    <span id="dzldf"><th id="dzldf"></th></span>
    <em id="dzldf"><form id="dzldf"></form></em>
    <address id="dzldf"></address>

    <form id="dzldf"><th id="dzldf"><progress id="dzldf"></progress></th></form>

      <form id="dzldf"></form>
      <em id="dzldf"><address id="dzldf"></address></em>