python爬虫爬取网页服务器IP
时间: 2023-11-11 10:13:16 浏览: 87
python爬虫爬取新闻示例.zip
Python爬虫可以通过以下步骤获取网页服务器IP:
1. 导入必要的库
```
import socket
import requests
```
2. 通过requests库发送网络请求,获取网页源代码
```
url = 'https://www.example.com'
response = requests.get(url)
html = response.content.decode('utf-8')
```
3. 通过socket库获取网页服务器IP
```
ip = socket.gethostbyname(socket.gethostname())
print("网页服务器IP地址:", ip)
```
完整代码示例:
```
import socket
import requests
url = 'https://www.example.com'
response = requests.get(url)
html = response.content.decode('utf-8')
ip = socket.gethostbyname(socket.gethostname())
print("网页服务器IP地址:", ip)
```
阅读全文