【问题标题】:why same request method has different html response structs?为什么相同的请求方法有不同的 html 响应结构?
【发布时间】: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,谢谢先生,但我的意思是错过的部分高于&lt;div&gt; part ,
  • 为什么不直接使用我们的 ASN API?听起来它更适合您,并且不会违反我们的服务条款,即抓取! :) 见ipinfo.io/developers

标签: python selenium request python-requests


【解决方案1】:

您可以通过以下方式获取所需的数据:

import requests
from lxml import html

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",
}
not_complete_result = requests.get('https://ipinfo.io/AS7018', headers=headers)
source = html.fromstring(not_complete_result.text)

print(source.xpath('//div[contains(@class, "card-header")]/h3/text()[1]')[0])
#  'AS7018 AT&T Services, Inc.'
for item in source.xpath('(//div[contains(@class, "card-body")])[1]//div[contains(@class, "col-")]/p'):
    print(item.text_content().strip())
# att.com
# United States
# 1996-07-30
# arin
# 80,925,184
# isp
# There are 78,008 domain names hosted across 34,656 IP addresses on this ASN.

【讨论】:

  • 您在这里通过 xpath 提供解决方案真是太好了,但现在让我感到困惑的是为什么有时我无法获得完整的页面资源...
  • @jiaJimmy ,这取决于你所说的'完整页面资源'...
  • 起初我使用正则表达式来匹配我所需的数据,如上 asn_code, name = re.search(r'&lt;h3 class="font-semibold m-0 t-xs-24"&gt;(?P&lt;ASN_CODE&gt;AS\d+) (?P&lt;NAME&gt;[\w.\s]+)&lt;/h3&gt;',s).groups() ,结果应该是 AS7018AT&amp;T Services, Inc. 但有时这不起作用,因为我无法完成not_complete_result.text的页面资源。
  • 另外,"AS7018 AT&amp;T Services, Inc." in not_complete_result.textFalse,它是有线的,你可以通过 xpath 找到它。
  • @jiaJimmy 1) 你不应该使用正则表达式来解析 HTML! 2) 在页面源文本显示为"AS7018 AT&amp;amp;T Services, Inc."。请注意与号字符。这就是为什么"AS7018 AT&amp;T Services, Inc." in not_complete_result.textFalse
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
  • 1970-01-01
  • 2014-09-10
  • 2013-03-01
  • 2019-05-07
  • 2020-07-07
相关资源
最近更新 更多