当前位置: 首页 > news >正文

企业建设网站价格公司搭建网站

企业建设网站价格,公司搭建网站,专业做网站系统,做网站的域名和空间是什么意思urllib库是什么?urllib库python的一个最基本的网络请求库,不需要安装任何依赖库就可以导入使用。它可以模拟浏览器想目标服务器发起请求,并可以保存服务器返回的数据。urllib库的使用:1、request.urlopen(1)只能传入url的方式from http.clie…

urllib库是什么?

urllib库python的一个最基本的网络请求库,不需要安装任何依赖库就可以导入使用。它可以模拟浏览器想目标服务器发起请求,并可以保存服务器返回的数据。

urllib库的使用:

1、request.urlopen

(1)只能传入url的方式

from http.client import HTTPResponse
from urllib import request
from urllib.request import Requesturl = "https://www.baidu.com"headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}response = request.urlopen(url) # type: HTTPResponseprint(response.read().decode("utf-8"))

(2) 传入Request对象和headers的方式

from http.client import HTTPResponse
from urllib import request
from urllib.request import Requesturl = "https://www.baidu.com"headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}req = Request(url, headers=headers)response = request.urlopen(req)  # type: HTTPResponseprint(response.read().decode("utf-8"))

2、request.urlretrieve

(1)简单使用,不能传入headers,只能传入url和保存的路径的方式

from http.client import HTTPResponse
from urllib import request
from urllib.request import Requesturl = "https://www.baidu.com"headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}# req = Request(url, headers=headers)
#
# response = request.urlopen(req)  # type: HTTPResponse
#
# print(response.read().decode("utf-8"))request.urlretrieve(url, "baidu.html")

(2)复杂使用,可以传入headers,传入url和保存的路径的方式

from urllib import requesturl = "https://www.baidu.com"
opener = request.build_opener()
opener.addheaders = ([("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36")])request.install_opener(opener)request.urlretrieve(url, "baidu.html")

额外的信息:

1、response的content-length

from http.client import HTTPResponse
from urllib import request
from urllib.request import Requesturl = "https://www.kuwo.cn/comment?type=get_comment&f=web&page=1&rows=5&digest=2&sid=93&uid=0&prod=newWeb&httpsStatus=1"headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}req = Request(url, headers=headers)
response = request.urlopen(req)  # type: HTTPResponsemeta = response.info()
# content-type
print(meta.get_content_type())
# content_charset
print(meta.get_content_charset())
# Content-Length
print(meta.get_all("Content-Length"))
print(response.getheader("Content-Length"))

urllib之parse模块的使用:

编码和解码

from urllib import parsedata = {"name": "王五","age": 31,"sex": "男","address": "北京市昌平区"
}# 参数编码
qs = parse.urlencode(data)
print(qs)# 解码
my_data = parse.parse_qs(qs)
print(my_data)

quote

起因:

在请求的url中,如果有汉字、空格或者特殊字符的时候,浏览器默认会将该字符进行urlencode()的处理,这样就可以正常的访问了!!!

代码实现:

错误代码:
from http.client import HTTPResponse
from urllib import parse, request
from urllib.request import Requesturl = "https://www.baidu.com/s?wd=%E6%9D%8E%E4%B8%80%E6%A1%90"headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
req = Request(url, headers=headers)
response = request.urlopen(req)  # type: HTTPResponse
# print(response.read().decode("utf-8"))url = "https://www.baidu.com/s?wd=李一桐"
req = Request(url, headers=headers)
response = request.urlopen(req)  # type: HTTPResponse
print(response.read().decode("utf-8"))
正确的代码:
from http.client import HTTPResponse
from urllib import parse, request
from urllib.request import Requesturl = "https://www.baidu.com/s?wd=%E6%9D%8E%E4%B8%80%E6%A1%90"headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36"
}
req = Request(url, headers=headers)
response = request.urlopen(req)  # type: HTTPResponse
# print(response.read().decode("utf-8"))url = "https://www.baidu.com/s?wd="
url = url + parse.quote("李一桐")
req = Request(url, headers=headers)
response = request.urlopen(req)  # type: HTTPResponse
print(response.read().decode("utf-8"))

urlparse、urlsplit的使用:

from urllib import parseurl = "https://www.baidu.com/login/title?id=123456&wd=hello#nav"
result = parse.urlparse(url)
print(result)
print("*" * 140)
print(result.scheme)
print(result.netloc)
print(result.path)
print(result.params)
print(result.fragment)
print(result.hostname)
print(result.port)print("*" * 140)result = parse.urlsplit(url)
print(result.scheme)
print(result.netloc)
print(result.path)
print(result.fragment)
print(result.hostname)
print(result.port)

http://www.hkea.cn/news/129761/

相关文章:

  • 风景旅游网页制作素材seo推广灰色词
  • 网站制作网站建设网页设计页面
  • 网站开发兼容极速字体颜色推荐seo平台优化服务
  • wordpress建站流量齐三seo顾问
  • 怎么看一个网站做没做竞价公司网站怎么建立
  • seo神马网站推广器怎么做神马搜索排名seo
  • 桂林漓江景区网站优化推广排名
  • 网站首页模板设计图网络推广平台代理
  • 一女被多男做的视频网站搜全网的浏览器
  • 建设公司网站费用电脑培训课程
  • 电子商务网站建设课后题女生学网络营销这个专业好吗
  • 新疆兵团建设网站商丘seo优化
  • 手机微信网站怎么做的软文发布网站
  • 传奇手游发布网站seo排名优化方式
  • 网站建设明细报价外链信息
  • 哪个网站做漫画可以有钱营销型网站设计
  • wordpress在线视频直播湖南正规关键词优化
  • 花木企业网站源码全网推广的方式
  • 网站开发商怎么关闭图片显示站长之家新网址
  • 灯饰如何做网站推广纯手工seo公司
  • 晋中公司做网站seo站长之家
  • 到哪里找人做网站优化seo培训班
  • 深圳网站开发哪家专业搜索到的相关信息
  • 湖北武汉网站制作引擎搜索下载
  • 做网站登录的需求分析seo点击排名工具有用吗
  • 诸暨住房和城乡建设委员会网站怎么制作网站?
  • 昆明cms建站模板视频号排名优化帝搜软件
  • 商务咨询网站源码重庆网站建设哪家好
  • 建设部网站从何时可以查询工程师证深圳全网推广服务
  • 网页制作工具的选择与网站整体风格是有关系的友情链接论坛