Python爬虫小白入门(九)Python 爬虫 – 使用requests抓取网页

Python中,requests库可用于向web服务器发出http请求,http请求有多种方式,例如,GET/POST/PUT/DELETE 等等。

这里将使用GET请求抓取页面:

import requests
page = requests.get("https://kevinhwu.github.io/demo/python-scraping/simple.html")
page

输出

<Response [200]>

发出请求之后,会返回一个响应对象。该对象包含一个status_code属性,表示页面访问是否成功:

page.status_code

输出

200

status_code为200,表示成功。关于http状态码,以2开头的状态代码通常表示成功,以4或5开头的代码表示错误,如需进一步了解,可参考相关资料。

可以使用content属性,打印出页面的HTML内容:

page.content

输出

b'<!DOCTYPE html>\n<html>\n<head>\n<title>\nA simple example page\n</title>\n</head>\n<body>\n<p>\nHere is some simple content for this page.\n</p>\n</body>\n</html>\n'

猜你喜欢

转载自www.cnblogs.com/huanghanyu/p/13175493.html