【问题标题】:How to send parameters with a GET request?如何使用 GET 请求发送参数?
【发布时间】:2013-07-07 22:09:44
【问题描述】:

我目前正在尝试向 YouTube 发出 GET 请求,以在其上搜索视频(使用 Python 以及 Requests 模块)。以下是我正在做的请求:

r = requests.get("http://www.youtube.com/", 
        params={
            "search_query": "Test"
        }).text

但是,在打印请求时,无论我将搜索查询更改为什么,它似乎都只是获取 YouTube 的主页。

有人知道为什么会这样吗?

谢谢

【问题讨论】:

    标签: python get request


    【解决方案1】:

    看youtube,搜索页面的url好像是

    http://www.youtube.com/results?search_query=test
    

    您缺少results 部分,这是您要查找的页面。

    【讨论】:

      【解决方案2】:

      使用 Youtube API 获取数据。

      # Import the modules
      import requests
      import json
      
      # Make it a bit prettier..
      print "-" * 30
      print "This will show the Most Popular Videos on YouTube"
      print "-" * 30
      
      # Get the feed
      r = requests.get("http://gdata.youtube.com/feeds/api/standardfeeds/top_rated?v=2&alt=jsonc")
      r.text
      
      # Convert it to a Python dictionary
      data = json.loads(r.text)
      
      # Loop through the result. 
      for item in data['data']['items']:
      
          print "Video Title: %s" % (item['title'])
      
          print "Video Category: %s" % (item['category'])
      
          print "Video ID: %s" % (item['id'])
      
          print "Video Rating: %f" % (item['rating'])
      
          print "Embed URL: %s" % (item['player']['default'])
      
          print
      

      通过http://www.pythonforbeginners.com/python-on-the-web/using-the-youtube-api/了解更多详情。

      【讨论】:

        【解决方案3】:

        试试这个看看你实际提交了什么 print r.url

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-10-18
          • 1970-01-01
          • 2021-05-23
          • 2018-04-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-20
          相关资源
          最近更新 更多