【问题标题】:Different content when accessing a website with requests使用请求访问网站时的不同内容
【发布时间】:2021-10-15 00:57:25
【问题描述】:

我正在尝试使用公司名称(例如“Google”)在 ARIN 中自动获取相应的句柄 ID。

https://search.arin.net/rdap/?query=google*

我幼稚的做法是使用requestsBeautifulSoup

import requests
from bs4 import BeautifulSoup

html = 'https://search.arin.net/rdap/?query='
comp = 'google*'

r = requests.get(html + comp)
soup = BeautifulSoup(r.text, 'html.parser')

#example search
search = soup.body.find_all(text = "Handle$")

但是,当我使用 requests 时,我得到的输出与我仅使用 Google Chrome 时不同。 requests返回的html代码不同,无法访问对应的句柄。

有人知道怎么改密码吗?

【问题讨论】:

    标签: python html beautifulsoup python-requests


    【解决方案1】:

    您在页面上看到的数据是从外部 API URL 加载的。你可以使用requests模块来模拟它:

    import json
    import requests
    
    api_url = "https://rdap.arin.net/registry/entities"
    params = {"fn": "google*"}
    
    data = requests.get(api_url, params=params).json()
    
    # pretty print the data:
    print(json.dumps(data, indent=4))
    

    打印:

    ...
    
            {
                "handle": "GF-231",
                "vcardArray": [
                    "vcard",
                    [
                        [
                            "version",
                            {},
                            "text",
                            "4.0"
                        ],
                        [
                            "fn",
                            {},
                            "text",
                            "GOOGLE FIBER INC"
                        ],
                        [
                            "adr",
                            {
                                "label": "3425 MALONE DR\nCHAMBLEE\nGA\n30341\nUnited States"
                            },
                            "text",
                            [
                                "",
                                "",
                                "",
                                "",
                                "",
                                "",
                                ""
                            ]
                        ],
                        [
                            "kind",
                            {},
                            "text",
                            "org"
                        ]
                    ]
                ],
    
    ...
    
    
    

    【讨论】:

    • 您是否使用 Chrome 网络标签找到了这个?最好解释一下您在回答中的表现。
    • @WilliamBradley 首先,我检查了信息是否在返回的 HTML 代码中(在 Firefox 中为Ctrl+U)。然后我打开了 Firefox 开发者工具 -> 网络选项卡,所有参数的 API URL 都在那里。
    • 非常感谢@AndrejKesely!真的很有帮助!
    猜你喜欢
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多