Requests库的使用-爬取HTML页面

def get_page(url):
    try:
        header = {
     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'}
        r = requests.get(url, headers=header)
        r.raise_for_status()
        print(r.text)
    except:
        print("出现异常")

if __name__ == '__main__':
    url = input("请输入网址: ")
    get_page(url)
# input: https://www.baidu.com
# output: html页面内容

你可能感兴趣的:(#,Python爬虫)