【发布时间】:2019-01-15 09:01:51
【问题描述】:
python3.6 + win10
当我从https://ipinfo.io/countries/us中抓取https://ipinfo.io/AS...之类的详细数据页面时,我从请求模块得到不同的结果,有时页面资源不完整。
如下,我举两个例子:
import requests
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
}
(1)请求页面https://ipinfo.io/AS13489(完成一个)
complete_result = requests.get('https://ipinfo.io/AS13489', headers=headers)
print(complete_result.text)
结果得到完整的html页面:
<!DOCTYPE html>
<html>
<head>
...
</body>
</html>
(2)请求页面https://ipinfo.io/AS7018(未完成)
not_complete_result = requests.get('https://ipinfo.io/AS7018', headers=headers)
print(not_complete_result.text)
结果只是得到不完整的html页面:
</tr>
<tr class="hidden">
...
</body>
</html>
(3) 除了selenium 在我的尝试中也没有用:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://ipinfo.io/AS7018')
browser.implicitly_wait(5)
print(browser.page_source)
结果不完整
256
</td>
</tr>
<tr class="hidden">
...
</iframe>
</html>
更新我所需的数据图片,我现在的困惑是有时这些部分数据会消失。
缺少部分 html 内容:
更新我的代码:
import re
import requests
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
}
# s = requests.get('https://ipinfo.io/AS7018', headers=headers).text
# not work , s get a not complete html cntent.
s = requests.get('https://ipinfo.io/AS13489', headers=headers).text
asn_code, name = re.search(r'<h3 class="font-semibold m-0 t-xs-24">(?P<ASN_CODE>AS\d+) (?P<NAME>[\w.\s]+)</h3>',s).groups()
country = re.search(r'.*href="/countries.*">(?P<COUNTRY>.*)?</a>',s).group("COUNTRY")
registry = re.search(r'Registry.*?pb-md-1">(?P<REGISTRY>.*?)</p>',s, re.S).group("REGISTRY").strip()
ip = re.search(r'IP Addresses.*?pb-md-1">(?P<IP>.*?)</p>',s, re.S).group("IP").strip()
print(asn_code, name, country, registry, ip)
# AS13489 EPM Telecomunicaciones S.A. E.S.P. Colombia lacnic 3,137,536
【问题讨论】:
-
对我来说很好用...当我检查 IP
"80,925,184 " in not_complete_result.text返回True,所以看起来所需的数据在页面源中 -
@Andersson,谢谢先生,但我的意思是错过的部分高于
<div>part , -
为什么不直接使用我们的 ASN API?听起来它更适合您,并且不会违反我们的服务条款,即抓取! :) 见ipinfo.io/developers
标签: python selenium request python-requests