requests

发布时间 2023-07-17 13:06:35作者: hacker_dvd
import requests
import re

url = 'https://www.baidu.com'
# get 方法是发送一个 get 请求,url 是关键字参数,表示请求的地址
# response 是一个响应对象,包含了服务器返回的所有信息
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
}
response = requests.get(url=url)
response.encoding = 'utf-8'
code = response.status_code  # 获取响应状态码
html = response.text         # 获取响应内容,但跟浏览器看到的不一样,因为没有设置请求头

print(code)
# print(html)

title = re.findall(r'<title>(.*?)</title>', html)
print(title)