第14.17节 爬虫实战3: request+BeautifulSoup实现自动获取本机上网公网地址
一、 引言
一般情况下,没有特殊要求的客户,宽带服务提供商提供的上网服务,给客户家庭宽带分配的地址都是一个宽带服务提供商的内部服务地址,真正对外访问时通过NAT进行映射到一个公网地址,如果我们想确认自己的公网地址还不是很容易,但百度提供了一项服务,在百度查询时输入IP就可以查到浏览器所在本机的公网IP,利用该功能我们就可以查到本机公网IP,本节老猿就是要request+BeatifulSoap实现本机公网地址解析。
二、 百度公网地址解析过程分析
- 按《第14.3节 使用google浏览器获取网站访问的http信息》方法老猿截获了在google浏览器中利用百度查询IP的请求报文和应答报文内容:
1)请求url为:
https://www.baidu.com/s?ie=utf-8&mod=1&isbd=1&isid=e58f9da20005786b&ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=IP&oq=IP&rsv_pq=e58f9da20005786b&rsv_t=d4227yacqXU1daqkMsArHRP4qBKnTUXvwdLeThY9w9ZbVPdHmXf4f6e3jik&rqlang=cn&rsv_enter=0&rsv_dl=tb&bs=IP&rsv_sid=1429_21095_29523_29519_28518_29098_29568_28831_29220_29072&_ss=1&clist=&hsug=&f4s=1&csor=2&_cr1=26201;
匿名方式下对应cookies的信息为(此处后面应用中需要请各位结合自己本机访问百度的cookies信息替换):
BAIDUID=05715D2A65E185B06ECD6CB62056F630:FG=1; BIDUPSID=05715D2A65E185B06ECD6CB62056F630; PSTM=1563498219; BD_UPN=12314353; ispeed_lsm=2; MCITY=-%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BD_HOME=1; H_PS_PSSID=1429_21095_29523_29519_29721_29568_29220_29072_29640; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BD_CK_SAM=1; PSINO=7; ZD_ENTRY=baidu; sug=0; sugstore=1; ORIGIN=0; bdime=0; COOKIE_SESSION=15_0_9_9_18_35_0_4_9_5_0_0_69285_0_0_0_1567933423_0_1567941823%7C9%23278256_7_1564840597%7C2; H_PS_645EC=143a70YMQeyHbLrfwaFTZLdW6usn4%2FtO8C6yxMcDcF7RjvdnVWhYHMxScchloWr5ipmN; WWW_ST=1567941827974
2)应答报文中关键标签信息为:
<table><tr><td>
<span class="c-gap-right">本机IP: 117.152.67.33</span>湖北省武汉市 移动
</td></tr></table>
2.构建http请求信息头后,利用request向百度发起IP地址查询的http请求;
3.使用BeatifulSoap解析应答信息。
三、 获取本机公网IP的完整代码
from bs4 import BeautifulSoup
import urllib.request
def getip():
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'}
cookie='BAIDUID=05715D2A65E185B06ECD6CB62056F630:FG=1; BIDUPSID=05715D2A65E185B06ECD6CB62056F630; PSTM=1563498219; BD_UPN=12314353; ispeed_lsm=2; MCITY=-%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BD_HOME=1; H_PS_PSSID=1429_21095_29523_29519_29721_29568_29220_29072_29640; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; BD_CK_SAM=1; PSINO=7; ZD_ENTRY=baidu; sug=0; sugstore=1; ORIGIN=0; bdime=0; COOKIE_SESSION=15_0_9_9_18_35_0_4_9_5_0_0_69285_0_0_0_1567933423_0_1567941823%7C9%23278256_7_1564840597%7C2; H_PS_645EC=143a70YMQeyHbLrfwaFTZLdW6usn4%2FtO8C6yxMcDcF7RjvdnVWhYHMxScchloWr5ipmN; WWW_ST=1567941827974'
try:
req = urllib.request.Request(url='https://www.baidu.com/s?ie=utf-8&mod=1&isbd=1&isid=05715D6F63032916&ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&tn=baiduhome_pg&wd=IP&rsv_spt=1&oq=IP&rsv_pq=a0a4fcb40013a6aa&rsv_t=143a70YMQeyHbLrfwaFTZLdW6usn4%2FtO8C6yxMcDcF7RjvdnVWhYHMxScchloWr5ipmN&rqlang=cn&rsv_enter=0&rsv_dl=tb&inputT=4024&rsv_sug4=4105&bs=IP&rsv_sid=undefined&_ss=1&clist=&hsug=&f4s=1&csor=2&_cr1=28287',headers=header)
text = urllib.request.urlopen(req,timeout=5).read().decode()
except Exception as e:
return "访问网络失败,失败原因:\n"+str(e)
else:
soup = BeautifulSoup(text, 'lxml')
ipinfo=soup.select('table>tr>td span.c-gap-right')
return ipinfo
getip()
老猿Python,跟老猿学Python!
博客地址:https://blog.csdn.net/LaoYuanPython
老猿Python博客文章目录:https://blog.csdn.net/LaoYuanPython/article/details/98245036
请大家多多支持,点赞、评论和加关注!谢谢!