python爬虫基础教程:requests库(二)代码实例

yipeiwu_com4年前Python爬虫

get请求

简单使用

import requests
'''
想要学习Python?Python学习交流群:973783996满足你的需求,资料都已经上传群文件,可以自行下载!
'''
response = requests.get("https://www.baidu.com/")
#text返回的是unicode的字符串,可能会出现乱码情况
# print(response.text)
 
#content返回的是字节,需要解码
print(response.content.decode('utf-8'))
 
 
# print(response.url)       #https://www.baidu.com/
# print(response.status_code)   #200
# print(response.encoding)    #ISO-8859-1

添加headers和params

import requests
 
params = {
  'wd':'python'
}
headers = {
  'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36'
}
 
response = requests.get("https://www.baidu.com/s",params=params,headers=headers)
 
#content返回的是字节,需要解码
with open('baidu.html','w',encoding='utf-8') as f:
  f.write(response.content.decode('utf-8'))

POST请求

爬去拉钩网职位信息

import requests
 
url = "https://www.lagou.com/jobs/positionAjax.json?city=%E5%8C%97%E4%BA%AC&needAddtionalResult=false"
 
data = {
  'first':'true',
  'pn':1,
  'kd':'python'
}
 
headers = {
  "User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36",
  "Referer":"https://www.lagou.com/jobs/list_python?city=%E5%8C%97%E4%BA%AC&cl=false&fromSearch=true&labelWords=&suginput="
}
 
response = requests.post(url,data=data,headers=headers)
# print(response.text)
print(type(response.text))    #<class 'str'>
print(type(response.json()))   #<class 'dict'>
 
print(response.json())      #获取为字典的形式

使用代理

import requests
 
proxy = {'http':'115.210.31.236.55:9000'}
 
response = requests.get("https://www.baidu.com/",proxies=proxy)
 
print(response.content.decode('utf-8'))

session登录

# _*_ coding:utf-8 _*_
 
import requests
 
# 1. 创建session对象,可以保存Cookie值
ssion = requests.session()
 
# 2. 处理 headers
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'}
 
# 3. 需要登录的用户名和密码
data = {"email":"158xxxxxxxx", "password":"pythonxxxxxxx"}
 
# 4. 发送附带用户名和密码的请求,并获取登录后的Cookie值,保存在ssion里
ssion.post("http://www.renren.com/PLogin.do", data = data)
 
# 5. ssion包含用户登录后的Cookie值,可以直接访问那些登录后才可以访问的页面
response = ssion.get("http://zhibo.renren.com/news/108")
 
# 6. 打印响应内容
print(response.text)
 

以上所述是小编给大家介绍的python爬虫基础教程:requests库(二)详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

利用selenium爬虫抓取数据的基础教程

写在前面 本来这篇文章该几个月前写的,后来忙着忙着就给忘记了。 ps:事多有时候反倒会耽误事。 几个月前,记得群里一朋友说想用selenium去爬数据,关于爬数据,一般是模拟访问某...

Python爬虫实现使用beautifulSoup4爬取名言网功能案例

本文实例讲述了Python爬虫实现使用beautifulSoup4爬取名言网功能。分享给大家供大家参考,具体如下: 爬取名言网top10标签对应的名言,并存储到mysql中,字段(名言,...

Python爬虫实现(伪)球迷速成

Python爬虫实现(伪)球迷速成

还有4天就世界杯了,作为一个资深(伪)球迷,必须要实时关注世界杯相关新闻,了解各个球队动态,这样才能在一堆球迷中如(大)鱼(吹)得(特)水(吹),迎接大家仰慕的目光! 给大家分享一个快...

Python实现的爬取网易动态评论操作示例

Python实现的爬取网易动态评论操作示例

本文实例讲述了Python实现的爬取网易动态评论操作。分享给大家供大家参考,具体如下: 打开网易的一条新闻的源代码后,发现并没有所要得评论内容。 经过学习后发现,源代码只是一个完整页面的...

python爬虫入门教程之点点美女图片爬虫代码分享

继续鼓捣爬虫,今天贴出一个代码,爬取点点网「美女」标签下的图片,原图。 # -*- coding: utf-8 -*- #----------------------------...