【问题标题】:Python request not showing desired resultPython请求未显示所需结果
【发布时间】:2019-03-22 12:44:26
【问题描述】:

我不是 python 专家,但这就是我对 python-requests 所做的。如果我提供 first_name、last_name 和域,我正在尝试调用这个 URL,它会为我提供用户的电子邮件 ID。

https://dry-tor-58240.herokuapp.com

但是,当我尝试使用 python 请求它时,我得到了 200 响应代码,但是当我将 response.text 转换为 Beautiful Soup 对象时,我在其中的任何地方都看不到电子邮件地址。

import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {"first_name":"nandish","last_name":"ajani","domain":"atyantik.com"}
r = requests.get("https://dry-tor-58240.herokuapp.com/", headers = headers, params = payload)
soup = BeautifulSoup(r.text, 'lxml')

谁能告诉我我做错了什么?

【问题讨论】:

    标签: python beautifulsoup python-requests


    【解决方案1】:

    应该是POST请求方式。这将返回一个json格式,所以我也使用了请求的.json()

    import requests
    from bs4 import BeautifulSoup
    
    request_url = 'https://dry-tor-58240.herokuapp.com/find'
    
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
    payload = {"first_name":"nandish","last_name":"ajani","domain":"atyantik.com"}
    jsonObj = requests.post(request_url, headers = headers, json = payload).json()
    

    输出:

    print (jsonObj['email'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2023-02-15
      • 2023-01-17
      • 2020-12-22
      • 1970-01-01
      相关资源
      最近更新 更多