当前位置: 移动技术网 > IT编程>脚本编程>Python > python爬虫(三)

python爬虫(三)

2018年10月03日  | 移动技术网IT编程  | 我要评论

初智齿音译歌词,running man e34,书拉密女小站

requests模块
这个库的标准文档有个极其幽默的地方就是它的中文翻译,我就截取个开头部分,如下图:

是不是很搞笑,在正文中还有许多,管中窥豹,可见一斑。通过我的使用,感觉requests库的确是给那些初学者,入门小白,非专业人士使用的,不会产生打人,砸键盘,脱发等一系列反人类行为,很好的使社会安全等级有又提升了一个档次,让人类社会向路不拾遗,夜不闭户又迈进了一步。(手动笑脸)
咱们先从安装resquests是库谈起,安装只需在windows的命令框中输入pip install requests,然后自动完成安装。这个库的许多方法我没有怎么用到,我只把一些入门及常用的方法介绍一遍,其他的请转移到www.python-requests.org这个阵地进行查看。
requests库使得人们可以非常方便的模拟浏览器的行为,去发送请求,并获得响应。
首先通过import在程序中导入requests模块:
import requests

接下来为get请求的实列:

只有一个url参数的get请求

response = requests.get('https://docs.python.org')

并设置超时时间timeout

response = requests.get('https://docs.python.org', timeout = 1)

查看发送请求的url地址

print('请求的url地址' + response.url)

查看当前返回状态编码,传输文件编码形式

print('返回状态编码' + response.encoding)

查看当前返回内容,text 返回字符串,content 返回字节流

print('text返回内容 ' + response.text)
print('content 返回内容 ' + str(response.content))

获取服务器返回的原始数据,html格式文件

data = requests.get('http://www,baidu.com', stream = true)

print(data.raw.read())

发送有多个参数的get请求

params = {'key1': 'value1', 'key2': 'value2'}
baidu_dictparams_response = requests.get('http://www.baidu.com', params=params)

发送list格式参数的get请求

print("发送list格式参数的get请求: ")
listparams = {'key1': 'value1', 'key2': ['value2', 'value3']}
listparams_response = requests.get('https://docs.python.org', params=listparams)

查看发送请求的url地址

print('带list参数的get请求地址为: ' + listparams_response.url)

post请求方式:

请求参数通过附加data方式传输

方式一,字典格式

post_response = requests.post('https://blog.csdn.net',
data = {'key': 'value'})
print('普通参数请求返回状态码: ' + str(post_response.status_code))

方式二 json格式

jsonparams = {'key': 'value'}
postjsonresponse = requests.post('https://blog.csdn.net',
data=json.dumps(jsonparams))
print('json参数请求返回状态码为: ' + str(postjsonresponse.status_code))

方式三 发送文件(该文件同级目录下需要有test.csv文件 )rb 只读模式 wb 若没有自动创建

files = {'file': open('d:\fff.csv', 'rb')}
fileresponse = requests.post('http://pythontab.com/posttest', files=files)

自定义headers 并发送,这是你自己浏览器的标识,在检查源码中可以获得

headers = {'user-agent': ' mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/64.0.3282.140 safari/537.36 edge/17.17134 '}
custom_headers_response = requests.get('https://blog.csdn.net', headers=headers)
print('自定义header发送请求状态码为:' + str(custom_headers_response.status_code))

带cookie参数的请求:

cookies_response = requests.get('https://blog.csdn.net')

自定义cookies 并发送

cookies = {'user-cookies': 'mycookies'}
custom_cookies_response = requests.get('http://pythontab.com/testlogin', cookies=cookies)
print('自定义cookies发送请求状态码为:' + str(custom_cookies_response.status_code))

通过requests获取session

session = requests.session()

举例:登录名 密码  key为登陆表单中对应的input的name值

login_data = {'email': 'email@example.com', 'password': 'password'}

发送数据

session.post("http://pythontab.com/testlogin", login_data)

获取发送的session

session_response = session.get('http://pythontab.com/notification/')
print('session请求返回的状态码为:' + str(session_response.status_code))

将页面以html的形式下载下来

utf-8可以使中文字显示出来

baiduhtmlcontent = requests.get("https://www.zhihu.com")
with open("百度.html", "wb") as html:
html.write(baiduhtmlcontent.content.decode(“utf-8”))
html.close()

这些代码没有什么特别的地方,只要按照requests模块中方法的要求,找准输入参数和输出内容,就能灵活运用这些内容。
这里说一下代理服务器的相关内容。
其实如果你想去用google搜索一些内容,上youtobe看看电影,与facebook的外国友人聊聊天,这些都需要一个叫做vpn的东西,就是将你的请求挂在在一个代理服务器的上面,然后代理服务器去向你指定的网站发出请求,获得一个缓存,并把这个缓存的数据返回给你。相当于一个互联网与你之间的一个中介,就像你租房子要通过58同城这样的中介网站一样。

大家可以关注我的微信公众号,如果有什么问题和我博客中的一些错误,请后台留言给我,谢谢!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网