python的requests模块爬取网页内容

注意:处理需要用户名密码认证的网站,需要auth字段。

# -*- coding:utf-8 -*-

import requests

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
}

newUrl ="https://www.freebuf.com/articles/system/187792.html"
#最简单的爬虫请求.也可以加上headers字段,防止部分网址的反爬虫机制
response = requests.get(newUrl)
#当爬取的界面需要用户名密码登录时候,构建的请求需要包含auth字段
#response = requests.get(newUrl,headers=headers,auth=('username','passsword'))
print(response.content.decode("utf-8"))#打印网页内容
#print(response.status_code)#浏览器返回的错误码,200表示成功

猜你喜欢

转载自www.cnblogs.com/taoyuanming/p/10768495.html