Python:这段Python代码(requests)有什么问题?

huangapple go评论62阅读模式
英文:

Python: What's wrong with this Python Code(requests)?

问题

Here's the translated code part:

import requests

def get_book():
    url = "http://search.dangdang.com/"
   
    response = requests.get(url, params={
        'key': 'c%D3%EF%D1%D4',
        'act': 'input'
    })
    
    print(response.text)

if __name__ == "__main__":
    get_book()

The issue in your code is that you used parms instead of params when passing parameters to the requests.get() method. I've corrected it to use params.

英文:
import requests
# import sys
# sys.setdefaultencoding('utf-8')
def get_book():
    #http://search.dangdang.com/?key=c%D3%EF%D1%D4&act=input
    url="http://search.dangdang.com/"
   
    rest=requests.get(url,parms={
        'key':'c%D3%EF%D1%D4',
        # "enc":"utf-8",
        # "wq":"haixian",
        'act':'input'
    })
    rest = requests.get(url)
    print(rest.text)
    # rest.json()
    # rest.content



    #requests.post()

if __name__=="__main__":
    get_book()

Process finished with exit code 1
TypeError: request() got an unexpected keyword argument 'parms'
It cannot work.
What's the problem with this code, and how can I fix it?

答案1

得分: 1

使用params而不是parms

import requests

def get_book():
    url = "http://search.dangdang.com/"

    rest = requests.get(url, params={
        'key': 'c%D3%EF%D1%D4',
        'act': 'input'
    })
    rest = requests.get(url)
    print(rest.text)

if __name__ == "__main__":
    get_book()
英文:

Use params instead of parms

import requests
# import sys
# sys.setdefaultencoding('utf-8')
def get_book():
    #http://search.dangdang.com/?key=c%D3%EF%D1%D4&act=input
    url="http://search.dangdang.com/"

    rest=requests.get(url,params={
        'key':'c%D3%EF%D1%D4',
        # "enc":"utf-8",
        # "wq":"haixian",
        'act':'input'
    })
    rest = requests.get(url)
    print(rest.text)
    # rest.json()
    # rest.content



    #requests.post()

if __name__=="__main__":
    get_book()

huangapple
  • 本文由 发表于 2020年1月7日 01:03:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616151.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定