1、参数不放在地址里
注意get的参数中,放入参的名字叫:paras,而post中叫“data:”
每次请求完成后都做关闭处理:rep.close(),避免多次请求后提示请求不到,因为请求链接太多没关闭的原因
- import requests
-
- url = "https://movie.douban.com/j/chart/top_list"
- data = {
- "type": 25,
- "interval_id": "100:90",
- "action": "",
- "start": 0,
- "limit": 20
- }
- headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
- }
-
- rep = requests.get(url,params=data,headers=headers ,verify=False)
- print(rep.json()[0]['rating']) # 这个返回字典格式,才能一个个取值,rep.text直接拿到字符串
- print(rep.request.url) # 查看地址方发现,参数会自己拼接到地址里面
-
- rep.close() # 每次请求后都关闭,避免后续连接太多无法请求
-
-
- >>>
- ['9.4', '50']
- https://movie.douban.com/j/chart/top_list?type=25&interval_id=100%3A90&action=&start=0&limit=20
2、参数放地址里
- import requests
-
- url = "https://movie.douban.com/j/chart/top_list?type=25&interval_id=100%3A90&action=&start=0&limit=20"
- headers = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
- }
-
- rep = requests.get(url,headers=headers ,verify=False)
- print(rep.json()[0]['rating']) # rep.json返回的是字典格式
- print(rep.request.url)
-
-
- >>>
- ['9.4', '50']
- https://movie.douban.com/j/chart/top_list?type=25&interval_id=100%3A90&action=&start=0&limit=20