Python http get
时间: 2024-03-28 10:32:47 浏览: 22
Python中可以使用`requests`库来发送HTTP GET请求。下是一个简单的示例代码:
```
import requests
url = "http://example.com" #换为你要请求的URL
response =.get(url)
print(response.status_code) # 打印响应状态码
print(response.text) # 打印响内容
```
在这个示例中我们使用`requests.get()`函数发送了一个GET请求,并将响应保存在`response`变量中。然后,我们可以通过`response.status_code`获取响应的状态码,通过`response.text`获取响应的内容。
如果你需要传递参数给GET请求,可以在URL中添加查询字符串参数,或者使用`params`参数传递一个字典。例如:
```python
import requests
url = "http://example.com"
params = {"key1": "value1", "key2": "value2"} # 替换为你的参数
response = requests.get(url, params=params)
print(response.status_code)
print(response.text)
```
这样就可以将参数添加到GET请求中了。
相关问题
python 爬虫 get
Python 爬虫中的 GET 方法是指通过 HTTP 协议中的 GET 请求方式获取网站上的信息。使用 Python 语言编写爬虫程序,通常需要用到 requests 库来发送 GET 请求。以下是一个使用 Python requests 库实现 GET 请求的示例代码:
```
import requests
response = requests.get("http://www.example.com")
print(response.status_code) # 打印响应状态码
print(response.text) # 打印响应内容
```
在上述代码中,我们使用了 requests 库中的 get() 方法,发送了一个 GET 请求,并将服务器返回的响应结果存储在 response 变量中。使用 response.status_code 可以获取响应状态码,使用 response.text 可以获取响应内容。
python http通信get示例
以下是一个 Python 的 HTTP GET 请求示例:
```python
import requests
url = 'http://example.com/api/data'
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get(url, params=params)
if response.status_code == 200:
print(response.json())
else:
print('请求失败')
```
其中,`url` 是请求的地址,`params` 是请求参数,`response` 是请求返回的响应对象。如果响应状态码为 200,则可以通过 `response.json()` 获取响应数据。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)