Python爬虫实现HTTP网络请求多种实现方式

时间:2021-05-23

1、通过urllib.requests模块实现发送请求并读取网页内容的简单示例如下:

#导入模块import urllib.request#打开需要爬取的网页response = urllib.request.urlopen('http:///>]>text: <!DOCTYPE html><!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8>………………(此处省略)content: b'<!DOCTYPE html>\r\n<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8>………………(此处省略)

以POST请求方式,发送HTTP网页请求的示例:

#导入模块import requests#表单参数data = {'word':'hello'}#对需要爬取的网页发送请求response = requests.post('http://httpbin.org/post',data=data)#以字节流形式打印网页源码print(response.content)

结果:

b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "word": "hello"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "10", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.23.0", \n "X-Amzn-Trace-Id": "Root=1-5ec3fc97-965139d919e5a08e8135e731"\n }, \n "json": null, \n "origin": "123.139.39.71", \n "url": "http://httpbin.org/post"\n}\n'

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章