ajax是什么
AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容。
在抓包工具里交xhr
ajax示例
- 破解百度翻译
代码:
# 破解百度翻译
# 有反爬
# coding:utf-8
import requests
import json
if __name__=="__main__":
url="https://fanyi.baidu.com/v2transapi?from=en&to=zh"
word=input("请输入单词")
data={
'query':word
}
headers={
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.39"
}
response=requests.post(url=url,data=data,headers=headers)
page_json=response.json()
with open("./index.json",'w',encoding='utf-8') as fp:
json.dump(page_json,fp,ensure_ascii=False)
print("爬取结束")
换一个网站
# 破解有道翻译
# 有反爬
# coding:utf-8
import requests
import json
if __name__=="__main__":
url="https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
word=input("请输入单词")
data={
'i':word
}
headers={
"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.39"
}
response=requests.post(url=url,data=data,headers=headers)
page_json=response.json()
with open("./index.json",'w',encoding='utf-8') as fp:
json.dump(page_json,fp,ensure_ascii=False)
print("爬取结束")
- 爬取豆瓣电影
代码:
# 爬取豆瓣电影
# coding=utf-8
# 有反爬
import requests
import json
if __name__=="__main__":
url="https://movie.douban.com/j/new_search_subjects"
data={
'sort':'U',
'range':'0,10',
'tags': '',
'start': '80',
'genres': '灾难'
}
headers={
'User-Agent':"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0"
}
resopnse=requests.post(url=url,data=data,headers=headers)
page_json=response.json()
with open("./index.json",'w') as fp:
json.dump(page_json,fp)
print('爬取结束')
- 爬取肯德基餐厅位置
代码:
# 肯德基餐厅位置
# coding=utf-8
# 有反爬
import requests
import json
if __name__=="__main__":
url="http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx"
data={
'cname':'' ,
'pid': '',
'keyword': '北京',
'pageIndex': '1',
'pageSize': '10'
}
headers={
'User-Agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36 Edg/100.0.1185.39"
}
resopnse=requests.post(url=url,data=data,headers=headers)
page_text=response.text
with open("./index.html",'w') as fp:
fp.write(page_text)
print('爬取结束')
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/245332.html