爬虫基础知识二

Posted -chenxs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬虫基础知识二相关的知识,希望对你有一定的参考价值。

发送get,post请求,获取响应

  • 安装requests模块:

    pip install requests
    

     

  • response=requests.get(url,headers=)#发送get请求,请求url地址对应的响应

  • response=requests.post(url,headers=,data=)  #发送post请求

response的方法

  • response.text

  • response.content.decode()

    • 把响应的二进制字节流转化为str类型

  • response.request.url  #发送请求的url地址
  • response.url  #响应的url地址
  • response.request.headers  #返回请求头
  • response.headers #返回响应请求

获取网页源码的三种正确打开方式

 

 

  • 1.response.content.decode()
  • 2.response.content.decode("gbk")
  • 3.response.text

发送带header的请求

 

  • 为了模拟浏览器,获取和浏览器一模一样的内容
url="https://www.xxx.com"
headers="User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
"Referer": "https://movie.douban.com/tag/"

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

  

 

以上是关于爬虫基础知识二的主要内容,如果未能解决你的问题,请参考以下文章

如何入门爬虫(基础篇)

如何入门爬虫(基础篇)

Python爬虫知识点二

Python爬虫入门二之爬虫基础了解

Python爬虫入门二之爬虫基础了解

爬虫基础二