爬虫——requests模块_ajax

ajax: 局部刷新,整个页面不刷新

get请求:

# -*- coding:utf-8 -*-
import urllib.request
import urllib.parse

url = "https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&"

page = input("第几页:")

num = 20

data = {
    "start":(int(page)-1)*num,
    "limit":num,
}
query_string = urllib.parse.urlencode(data)

url += query_string

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
}

request = urllib.request.Request(url,headers=headers)

response = urllib.request.urlopen(request)

print(response.read().decode())

post请求:

import urllib.request
import urllib.parse

url = "http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname"

fromdata = {
    'cname': '北京',
    'pid':'',
    'pageIndex': '2',
    'pageSize': '10',
}

headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36",
}

request = urllib.request.Request(url,headers=headers)

fromdata = urllib.parse.urlencode(fromdata).encode()

response = urllib.request.urlopen(request,data=fromdata)

print(response.read().decode())

猜你喜欢

转载自blog.csdn.net/weixin_42598585/article/details/87891265