使用requests库向对应网站(https://www.bilibili.com/)发起请求,并通过res.status_code的值来判断请求是否成功
时间: 2023-12-06 18:43:29 浏览: 30
可以使用下面的代码来实现:
```python
import requests
url = 'https://www.bilibili.com/'
res = requests.get(url)
if res.status_code == 200:
print('请求成功!')
else:
print('请求失败!')
```
在这里,我们使用requests库发送了一个GET请求到B站的主页,然后通过res.status_code来判断请求是否成功。如果状态码为200,就表示请求成功。
相关问题
使用requests库爬取https://www.xynu.edu.cn/
好的,以下是使用requests库爬取https://www.xynu.edu.cn/的示例代码:
```python
import requests
url = 'https://www.xynu.edu.cn/'
response = requests.get(url)
if response.status_code == 200:
print(response.text)
else:
print('请求失败')
```
这段代码会发送一个GET请求到https://www.xynu.edu.cn/,并将响应内容打印出来。如果请求成功,响应状态码会是200,否则就会输出"请求失败"。你可以根据自己的需要对这段代码进行修改和扩展。
使用requests库爬取https://www.xytc.edu.cn/
好的,以下是Python代码示例:
```python
import requests
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 设置目标URL
url = 'https://www.xytc.edu.cn/'
# 发送请求
response = requests.get(url, headers=headers, verify=False)
# 输出结果
print(response.text)
```
这段代码使用requests库发送了一个HTTP GET请求,请求的URL是信阳师范学院的主页,设置了一个User-Agent的请求头,以模拟浏览器发送请求。由于该网站使用了HTTPS协议,因此需要设置verify参数为False,以忽略SSL证书验证。接着,获取响应内容,最后输出响应的HTML文本。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)