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

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

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

问题

Here's the translated code part:

  1. import requests
  2. def get_book():
  3. url = "http://search.dangdang.com/"
  4. response = requests.get(url, params={
  5. 'key': 'c%D3%EF%D1%D4',
  6. 'act': 'input'
  7. })
  8. print(response.text)
  9. if __name__ == "__main__":
  10. 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.

英文:
  1. import requests
  2. # import sys
  3. # sys.setdefaultencoding('utf-8')
  4. def get_book():
  5. #http://search.dangdang.com/?key=c%D3%EF%D1%D4&act=input
  6. url="http://search.dangdang.com/"
  7. rest=requests.get(url,parms={
  8. 'key':'c%D3%EF%D1%D4',
  9. # "enc":"utf-8",
  10. # "wq":"haixian",
  11. 'act':'input'
  12. })
  13. rest = requests.get(url)
  14. print(rest.text)
  15. # rest.json()
  16. # rest.content
  17. #requests.post()
  18. if __name__=="__main__":
  19. 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

  1. import requests
  2. def get_book():
  3. url = "http://search.dangdang.com/"
  4. rest = requests.get(url, params={
  5. 'key': 'c%D3%EF%D1%D4',
  6. 'act': 'input'
  7. })
  8. rest = requests.get(url)
  9. print(rest.text)
  10. if __name__ == "__main__":
  11. get_book()
英文:

Use params instead of parms

  1. import requests
  2. # import sys
  3. # sys.setdefaultencoding('utf-8')
  4. def get_book():
  5. #http://search.dangdang.com/?key=c%D3%EF%D1%D4&act=input
  6. url="http://search.dangdang.com/"
  7. rest=requests.get(url,params={
  8. 'key':'c%D3%EF%D1%D4',
  9. # "enc":"utf-8",
  10. # "wq":"haixian",
  11. 'act':'input'
  12. })
  13. rest = requests.get(url)
  14. print(rest.text)
  15. # rest.json()
  16. # rest.content
  17. #requests.post()
  18. if __name__=="__main__":
  19. 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:

确定