Python requests添加User Agent

2024-06-11阅读数:255
上一篇:Python requests 库爬取网页
下一篇:Python request带Payload POST
# coding=utf-8
import requests
# 请求网页的函数,参数url为网页的完整链接,函数返回请求回来的HTML代码
def get_html(url):
    # 准备好User-Agent到变量myheaders
    myheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'}

    response = requests.get(url, headers=myheaders) #把上面准备好的User-Agent变量赋值给requests.get的headers参数 
    status_code = response.status_code 
    if status_code != 200:
        print(f'request failed: {status_code}')
        return ''

    return response.text

if __name__ == '__main__':
    content = get_html('http://www.spiderbuf.cn')
    print(content)