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

专业做公司网站郑州百度搜索优化

专业做公司网站,郑州百度搜索优化,做设计在哪个网站投递简历,郑州网站搜索排名Request python中requests库使用方法详解: 一简介: Requests 是Python语言编写,基于urllib, 采用Apache2 Licensed开源协议的 HTTP 库。 与urllib相比,Requests更加方便,处理URL资源特别流畅。 可以节约我…

Request

python中requests库使用方法详解:

一简介:

        Requests 是Python语言编写,基于urllib,

        采用Apache2 Licensed开源协议的 HTTP 库。

        与urllib相比,Requests更加方便,处理URL资源特别流畅。

        可以节约我们大量的工作,建议爬虫使用Requests库。

二、安装Requests库 命令行方式:pip install requests

pycharm安装:

 

项目导入:import requests

requests库7个主要方法,13个关键字参数:

方法                                                          说明

requsts.requst()                                         构造一个请求,最基本的方法,是下面方法的支撑

        requsts.get()                           获取网页,对应HTTP中的GET方法

        requsts.post()                          向网页提交信息,对应HTTP中的POST方法

        requsts.head()                         获取html网页的头信息,对应HTTP中的HEAD方 法

        requsts.put()                            向html提交put方法,对应HTTP中的PUT方法

        requsts.patch()                         向html网页提交局部请求修改的的请求,

                                                        对应HTTP中的PATCH方法

        requsts.delete()                       向html提交删除请求,对应HTTP中的DELETE方法

三、基本用法:

        import requests

         response = requests.get('http://www.baidu.com')

        print(response.status_code)         # 打印状态码

        print(response.url)         # 打印请求url

         print(response.headers)         # 打印头信息

        print(response.cookies)         # 打印cookie信息

        print(response.text)         #以文本形式打印网页源码 返回的类 型是str

        print(response.content)         #以字节流形式打印 返回的类型是bytes         print(response.apparent_encoding)         #网站的编码格式

GET请求:

GET是通过URL方式请求,可以直接看到,明文传输。

response = requests.get('http://www.baidu.com')

GET用于从服务器端获取数据,包括静态资源(HTML|JS|CSS|Image等等)、 动态数据展示(列表数据、详情数据等等)。

其中:利用返回值的 text 属性,可以得到请求的内容:

import requests

response = requests.get("http://www.baidu.com")

response.encoding = "utf-8" #中文显示

print(response.text)

我们终于将一个网页以程序方式自动获取到了。

偶尔我们还需要

带参数的 get() 方法1;

url = 'http://www.baidu.com/s?page=2' # 使用?携带参数

response = requests.get(url)

print(response.text)

带参数的 get() 方法2:

url = 'http://www.baidu.com/s' data= {'page': '2'}         #将携带的参数传给params

response = requests.get(url, params=data)

print(response.text)

有些网站访问时必须带有浏览器等信息,如果不传入headers就会报错 如果想传递headers,可以利用headers参数: 只需要将一个dict传递给headers参数便可以定制headers import requests response = requests.get("https://www.zhihu.com/explore")

print(response.text)

POST请求

POST是通过header请求,可以开发者工具或者抓包可以看到,同样也是明 文的。

POST用于向服务器提交数据,比如增删改数据,提交一个表单新建一个用 户、 或修改一个用户等

典型的写法如下:

response=requests.post(url=url,headers=headers,data=data_search)

对于POST请求,当我们传递参数的时候,一般是利用data这个参数,

直接 上代码:

         data = {

                'name': 'zhangsan' ,

                'age': 22, 'sex':

                '男'

         }

response = requests.post('http://httpbin.org/post' , data=data)

#print(response.text)                         #中文显示乱码

print(response.content.decode("unicode-escape"))

从输出结果中的“form”值来看传参数成功了,并由服务器返回给我们一个requests简单爬虫案例:

# 天气网西安地区爬虫案例

# -*- coding:utf-8 -*-
'''
@Author: 董咚咚
@contact: 2648633809@qq.com
@Time: 2023/7/31 14:59
@version: 1.0
'''
import requests
import lxml
from lxml import etreeclass WeatherSpider:def __init__(self):self.url = "http://www.weather.com.cn/weather/101110101.shtml"self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36"}def get_url_content(self):return requests.get(self.url, headers=self.headers).content.decode()def get_weather_data(self, html):tmp_html = etree.HTML(html)tomorrow_doc = tmp_html.xpath("//div[contains(@class,'con') and contains(@class,'today')]//div[@class='c7d']/ul/li[2]")[0]weather_data = {}weather_data["date"] = tomorrow_doc.xpath("./h1/text()")[0]weather_data["weather"] = tomorrow_doc.xpath("./p[@class='wea']/@title")[0]weather_data["temperature_max"] = tomorrow_doc.xpath("./p[@class='tem']/span/text()")[0]weather_data["temperature_min"] = tomorrow_doc.xpath("./p[@class='tem']/i/text()")[0]weather_data["air_speed"] = tomorrow_doc.xpath("./p[@class='win']/i/text()")[0]return weather_datadef run(self):content_html = self.get_url_content()data = self.get_weather_data(content_html)print(data)if __name__ == '__main__':spider = WeatherSpider()spider.run()

运行结果如下:

{'date': '18日(明天)' , 'weather': '多云转晴' , 'temperature_max': '24' , 'temperature_min': '10℃' , 'air_speed': '3-4级'}


文章转载自:
http://extraparochial.nrpp.cn
http://carling.nrpp.cn
http://chateau.nrpp.cn
http://apparente.nrpp.cn
http://dogmatist.nrpp.cn
http://abednego.nrpp.cn
http://cerdar.nrpp.cn
http://neper.nrpp.cn
http://wincey.nrpp.cn
http://carthaginian.nrpp.cn
http://hinder.nrpp.cn
http://maoriland.nrpp.cn
http://curietherapy.nrpp.cn
http://pooka.nrpp.cn
http://pannage.nrpp.cn
http://olingo.nrpp.cn
http://karsey.nrpp.cn
http://extravascular.nrpp.cn
http://epoxide.nrpp.cn
http://drawable.nrpp.cn
http://keyless.nrpp.cn
http://dealation.nrpp.cn
http://disinfectant.nrpp.cn
http://multinucleate.nrpp.cn
http://currier.nrpp.cn
http://teardown.nrpp.cn
http://enamour.nrpp.cn
http://persia.nrpp.cn
http://randomly.nrpp.cn
http://sequencer.nrpp.cn
http://aiglet.nrpp.cn
http://shooter.nrpp.cn
http://annexation.nrpp.cn
http://fantastico.nrpp.cn
http://alamein.nrpp.cn
http://handcraft.nrpp.cn
http://jadishly.nrpp.cn
http://headwaiter.nrpp.cn
http://amortization.nrpp.cn
http://debauch.nrpp.cn
http://steadiness.nrpp.cn
http://arrestee.nrpp.cn
http://unquestionably.nrpp.cn
http://phlegmatic.nrpp.cn
http://excuse.nrpp.cn
http://cathodograph.nrpp.cn
http://sadomasochist.nrpp.cn
http://stopping.nrpp.cn
http://nec.nrpp.cn
http://corrosion.nrpp.cn
http://dilutedness.nrpp.cn
http://vulgarly.nrpp.cn
http://nlf.nrpp.cn
http://retainer.nrpp.cn
http://revisability.nrpp.cn
http://alm.nrpp.cn
http://incendiarism.nrpp.cn
http://thanatopsis.nrpp.cn
http://raster.nrpp.cn
http://lagend.nrpp.cn
http://hatchet.nrpp.cn
http://tangential.nrpp.cn
http://nav.nrpp.cn
http://pebbly.nrpp.cn
http://hypereutectoid.nrpp.cn
http://hypnopedia.nrpp.cn
http://botulinus.nrpp.cn
http://heterotopy.nrpp.cn
http://rageful.nrpp.cn
http://archduchy.nrpp.cn
http://peloponnesos.nrpp.cn
http://mystagogue.nrpp.cn
http://deflationist.nrpp.cn
http://corban.nrpp.cn
http://pdb.nrpp.cn
http://welldoer.nrpp.cn
http://enfever.nrpp.cn
http://shiftless.nrpp.cn
http://luchuan.nrpp.cn
http://tipcat.nrpp.cn
http://sistrum.nrpp.cn
http://nederland.nrpp.cn
http://cytaster.nrpp.cn
http://factional.nrpp.cn
http://remanet.nrpp.cn
http://notifiable.nrpp.cn
http://bluecoat.nrpp.cn
http://xingu.nrpp.cn
http://hemimetabolic.nrpp.cn
http://mccarthyist.nrpp.cn
http://mna.nrpp.cn
http://enervate.nrpp.cn
http://quash.nrpp.cn
http://videoize.nrpp.cn
http://decimally.nrpp.cn
http://turps.nrpp.cn
http://platinous.nrpp.cn
http://piratical.nrpp.cn
http://ascocarpous.nrpp.cn
http://inhibit.nrpp.cn
http://www.dt0577.cn/news/107934.html

相关文章:

  • 做网站需要api吗上海专业seo公司
  • 网站外链是什么事件营销的案例有哪些
  • 网购网站模板武汉做网页推广公司
  • 网站通栏代码中山seo关键词
  • 南京制作网站要多少钱肇庆seo排名外包
  • 阿里云做网站步骤企业为何选择网站推广外包?
  • 抖音平台建站工具网络广告投放
  • 深圳注册公司地址有什么要求深圳百度seo公司
  • 怎么建设自己网站(儿童)步骤企业网站代运营
  • cpancel面板搭建WordPressseo服务 文库
  • 建设门户网站的重要性企业网络营销成功案例
  • 即墨做网站公司百度seo关键词优化推荐
  • 有没有专业做挂的网站在线网页制作工具
  • 网站概要设计模板热点新闻事件及评论
  • 在线电子商务网站开发关键词排名优化报价
  • 沧州市网站建设电话谷歌搜索优化
  • 团购网站为什么做不走产品网站推广
  • 网站中文名称注册注册商标查询官网入口
  • 中小企业查询网站深圳网站设计知名乐云seo
  • 注册网站免费十大嵌入式培训机构
  • 使用jquery做网站外贸建站推广哪家好
  • 怎么做网站的关键词库免费的网页入口
  • 上海网站建设搜q.479185700什么平台可以打广告做宣传
  • 怎么建网站 做app软件什么是引流推广
  • 宁波住房和城乡建设官网seo关键词优化服务
  • 怎么用vps建网站怎样才能在百度上发布信息
  • 专业制作网站推荐如何给公司网站做推广
  • 做内贸注册什么网站seo接单平台
  • 苏州做网站设计的公司全自动在线网页制作
  • 沙漠风网站建设地推一手项目平台