python知识
棉马咪
这个作者很懒,什么都没留下…
展开
-
python爬虫-BeautifulSoup
BeautifulSoup是将复杂的HTML文档转化为一个复杂的树形结构,每个节点都是python对象,可以归纳为以下四种:TagNavigableStringBeautifulSoupComment注:以下的baidu.html是以baidu首页为例。1.文档内容的获取Tag找到网页对应的标签及其内容file = open("baidu.html", 'r', encoding='utf-8')html = file.read()bs = BeautifulSoup(html,原创 2021-03-02 15:53:05 · 97 阅读 · 0 评论 -
python爬虫-urllib
urllib用法发送get请求发送post请求发送请求超时处理避免被网站识破发送get请求get请求可以不用附带任何的信息,直接发送即可import urllib.requestimport urllib.parseresponse = urllib.request.urlopen("https://www.baidu.com")print(response.read().decode("utf-8")) # utf-8可以让返回的内容不会乱码发送post请求post请求需要发送一个用户的原创 2021-03-01 17:59:53 · 105 阅读 · 1 评论 -
python-异常错误
错误的类型有很多种,例如NameError, IOError等,可以用Exception来囊括所有的错误类型。result用来记录错误的具体信息。try: print(num)except Exception as result: print("产生错误了,错误的类型是{}".format(result))result-------------------------------产生错误了,错误的类型是name 'num' is not defined...原创 2021-02-28 16:19:11 · 149 阅读 · 2 评论 -
python-get
常用于字典的访问age = {"小明":5, "小黄":3, "小张":4}print(age.get("小红", "没有找到"))result--------------------------没有找到原创 2021-02-28 15:19:48 · 60 阅读 · 0 评论 -
python-count
功能:统计某个内容一共出现了多少次a = [1,2,3,4,5]print(a.count(1))result-----------------------1a = [1,1,3,4,5]print(a.count(1))result-----------------------2原创 2021-02-28 14:47:56 · 87 阅读 · 0 评论 -
python-index
功能:在指定范围内查找内容,若在返回下标,否则报错a = [1,2,3,4,5]a.index(3, 0, 2)resutl-------------------------ValueError: 3 is not in lista = [1,2,3,4,5]print(a.index(3, 0, 3))resutl-------------------------2原创 2021-02-28 14:46:31 · 114 阅读 · 0 评论 -
python文件如何设置文件头部注释
展示内容如下具体操作方法:File→\rightarrow→Settings→\rightarrow→Editor→\rightarrow→File and Code Templates找到Python Script,输入以下内容,即可#-*- utf-8 -*-#@Time: ${DATE} ${TIME}#@Author: Frank Huang#@File: ${NAME}.py#@Software: ${PRODUCT_NAME}...原创 2021-02-28 12:25:47 · 1117 阅读 · 1 评论 -
python_深拷贝,浅拷贝
深拷贝:开辟了新的地址空间浅拷贝:仅仅拷贝引用关系import copya, b = [11, 22], [33, 44]c = [a, b]d = copy.copy(c)print(id(d[0]))print(id(c[0]))print(id(a))结果----------------------------275092775411227509277541122750927754112发现使用copy.copy()的时候,d复制下来的元素,都还是a的地址impor.原创 2021-02-08 20:49:57 · 74 阅读 · 1 评论