Python中,我们可以使用requests模块来发送HTTP请求并获取响应。下面是一个简单的例子:
import requests
response = requests.get('https://www.example.com')
print(response.text)
上面的代码中,我们使用了requests.get()方法来发送GET请求,并将响应存储在response变量中。我们可以通过访问response.text属性来获取响应内容。
如果你需要发送其他类型的请求,例如POST请求,你可以使用requests.post()方法:
import requests
data = {'username': 'john', 'password': '123456'}
response = requests.post('https://www.example.com/login', data=data)
print(response.text)
上面的代码中,我们使用了requests.post()方法来发送POST请求,并将数据存储在data字典中。我们可以通过访问response.text属性来获取响应内容。
除了get()和post()方法之外,requests模块还提供了其他方法,例如put()、delete()等。你可以根据自己的需求选择合适的方法。
来说,在Python中获取HTTP请求非常简单,只需要使用requests模块即可轻松完成。