当前位置: 首页 > news >正文

wordpress自定义字段类型优化内容

wordpress自定义字段类型,优化内容,自制网站要钱吗,网站开发主管岗位职责目录 一、 ajax的get请求豆瓣电影第一页 二、ajax的get请求豆瓣电影前十页 三、ajax的post请求肯德基官网 一、 ajax的get请求豆瓣电影第一页 目标:获取豆瓣电影第一页的数据,并保存为json文件 设置url,检查 --> 网络 --> 全部 -…

目录

一、 ajax的get请求豆瓣电影第一页

二、ajax的get请求豆瓣电影前十页

三、ajax的post请求肯德基官网


一、 ajax的get请求豆瓣电影第一页

目标:获取豆瓣电影第一页的数据,并保存为json文件

设置url,检查 -->  网络 -->  全部  --> top_list --> 标头  --> 请求URL

完整代码:

import urllib.request"""
# get请求
# 获取豆瓣电影第一页的数据,并保存为json文件
"""
url = 'https://movie.douban.com/j/chart/top_list?type=17&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/118.0.0.0 Safari/537.36 Edg/118.0.2088.76"
}# 请求对象的定制
request = urllib.request.Request(url, headers=headers)# 获取响应的数据
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)# 数据下载到本地
with open('douban.json','w', encoding='utf-8') as file:file.write(content)# import json
# with open('douban.json','w', encoding='utf-8') as file:
#    json.dump(content, file, ensure_ascii=False)
"""
通常是因为默认情况下,json.dump() 使用的编码是 ASCII,不支持包含非ASCII字符(如中文)的文本。为了在 JSON 文件中包含中文字符,你可以指定 ensure_ascii=False 参数,以确保不将中文字符转换为 Unicode 转义序列。
"""

二、ajax的get请求豆瓣电影前十页

 目标:下载豆瓣电影前十页的数据

知识点:问题的关键在于观察url的规律,然后迭代获取数据

1.设置url

找规律

点击top_list获取第一页的request url,复制url,点击清空,下拉滚动条,再次出现top_list时复制第二页的request url,重复操作,我们可以找到规律。

 观察链接,可以看到page和start之间的关系

# url 规律
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=0&limit=20'
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=20&limit=20'
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=40&limit=20'
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=60&limit=20'# page   1  2  3  4
# start  0  20 40 60

2.定义请求对象定制的函数

def create_request(page):base_url = 'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&'data={'start':(page-1)*20,'limit':20}data = urllib.parse.urlencode(data)url = base_url+dataheaders = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76"}# 请求对象的定制request = urllib.request.Request(url, headers=headers)return request

3.定义获取响应的数据的函数

def get_content(request):# 获取响应的数据response = urllib.request.urlopen(request)content = response.read().decode('utf-8')return content

4.定义下载函数

def down_load(content,page):with open('daouban_'+str(page)+'.json','w',encoding='utf-8') as file:file.write(content)

5.调用这些函数,完成数据抓取

start_page = int(input("请输入起始页码"))
end_page = int(input("请输入结束的代码"))for page in range(start_page, end_page+1):# 请求对象的定制request = create_request(page)# 获取响应的数据content = get_content(request)# 下载down_load(content,page)print(f"done_{page}")

完整代码

import urllib.request
import urllib.parse
# url 规律
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=0&limit=20'
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=20&limit=20'
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=40&limit=20'
'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&start=60&limit=20'# page   1  2  3  4
# start  0  20 40 60# 全部工作:下载豆瓣电影前十页的数据
# 请求对象的定制
# 获取响应的数据
# 下载数据def create_request(page):base_url = 'https://movie.douban.com/j/chart/top_list?type=17&interval_id=100%3A90&action=&'data={'start':(page-1)*20,'limit':20}data = urllib.parse.urlencode(data)url = base_url+dataheaders = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76"}# 请求对象的定制request = urllib.request.Request(url, headers=headers)return requestdef get_content(request):# 获取响应的数据response = urllib.request.urlopen(request)content = response.read().decode('utf-8')return contentdef down_load(content,page):with open('daouban_'+str(page)+'.json','w',encoding='utf-8') as file:file.write(content)start_page = int(input("请输入起始页码"))
end_page = int(input("请输入结束的代码"))for page in range(start_page, end_page+1):# 请求对象的定制request = create_request(page)# 获取响应的数据content = get_content(request)# 下载down_load(content,page)print(f"done_{page}")

三、ajax的post请求肯德基官网

 目标:查询肯德基某地区餐厅前十页的信息

1.设置url

进入肯德基官网,点击餐厅查询,右键检查

网络 --> 名称 --> 标头 --> 请求URL

然后点击清空(左上角的 ∅),点击第二页,再次获取链接信息及负载中的表单数据,找规律

可以找到如下规律

pageIndex就是页码

# 第1页
# cname: 深圳
# pid:
# pageIndex: 1
# pageSize: 10# 第2页
# http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname
# cname: 深圳
# pid:
# pageIndex: 2
# pageSize: 10

2.定义请求对象地址的函数

def create_request(page):base_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'data={'cname': '深圳','pid': '','pageIndex': page,'pageSize': '10'}data = urllib.parse.urlencode(data).encode('utf-8')headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76"}request = urllib.request.Request(base_url, data, headers)return  request

3.获取网页源码

def get_content(request):response = urllib.request.urlopen(request)content = response.read().decode('utf-8')return content

4.下载

def download(content, page):with open ('kfc_'+str(page)+'.json','w',encoding='utf-8') as file:file.write(content)

5.调用函数

start_page = int(input('请输入起始页码'))
end_page = int(input('请输入结束页码'))
for page in range(start_page, end_page+1):# 请求对象定制response = create_request(page)# 获取网页源码content = get_content(response)# 下载1download(content, page)

完整代码:

import urllib.request
import urllib.parse# 第1页
# cname: 深圳
# pid:
# pageIndex: 1
# pageSize: 10# 第2页
# http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname
# cname: 深圳
# pid:
# pageIndex: 2
# pageSize: 10def create_request(page):base_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'data={'cname': '深圳','pid': '','pageIndex': page,'pageSize': '10'}data = urllib.parse.urlencode(data).encode('utf-8')headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76"}request = urllib.request.Request(base_url, data, headers)return  requestdef get_content(request):response = urllib.request.urlopen(request)content = response.read().decode('utf-8')return contentdef download(content, page):with open ('kfc_'+str(page)+'.json','w',encoding='utf-8') as file:file.write(content)start_page = int(input('请输入起始页码'))
end_page = int(input('请输入结束页码'))
for page in range(start_page, end_page+1):# 请求对象定制response = create_request(page)# 获取网页源码content = get_content(response)# 下载1download(content, page)

参考

尚硅谷Python爬虫教程小白零基础速通(含python基础+爬虫案例)


文章转载自:
http://www.morning.prgdy.cn.gov.cn.prgdy.cn
http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn
http://www.morning.nfsrs.cn.gov.cn.nfsrs.cn
http://www.morning.banzou2034.cn.gov.cn.banzou2034.cn
http://www.morning.nshhf.cn.gov.cn.nshhf.cn
http://www.morning.bfybb.cn.gov.cn.bfybb.cn
http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn
http://www.morning.nshhf.cn.gov.cn.nshhf.cn
http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn
http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn
http://www.morning.hdscx.cn.gov.cn.hdscx.cn
http://www.morning.srjgz.cn.gov.cn.srjgz.cn
http://www.morning.lcjw.cn.gov.cn.lcjw.cn
http://www.morning.vuref.cn.gov.cn.vuref.cn
http://www.morning.nwjzc.cn.gov.cn.nwjzc.cn
http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn
http://www.morning.rywn.cn.gov.cn.rywn.cn
http://www.morning.shprz.cn.gov.cn.shprz.cn
http://www.morning.jydky.cn.gov.cn.jydky.cn
http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn
http://www.morning.srnhk.cn.gov.cn.srnhk.cn
http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn
http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn
http://www.morning.rbmm.cn.gov.cn.rbmm.cn
http://www.morning.mqfw.cn.gov.cn.mqfw.cn
http://www.morning.znqxt.cn.gov.cn.znqxt.cn
http://www.morning.ysjjr.cn.gov.cn.ysjjr.cn
http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn
http://www.morning.rlbc.cn.gov.cn.rlbc.cn
http://www.morning.wklrz.cn.gov.cn.wklrz.cn
http://www.morning.jxltk.cn.gov.cn.jxltk.cn
http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn
http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn
http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn
http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn
http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn
http://www.morning.gllhx.cn.gov.cn.gllhx.cn
http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn
http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn
http://www.morning.rnmc.cn.gov.cn.rnmc.cn
http://www.morning.trqsm.cn.gov.cn.trqsm.cn
http://www.morning.hrpmt.cn.gov.cn.hrpmt.cn
http://www.morning.wynqg.cn.gov.cn.wynqg.cn
http://www.morning.sypzg.cn.gov.cn.sypzg.cn
http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn
http://www.morning.lgmty.cn.gov.cn.lgmty.cn
http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn
http://www.morning.pcbfl.cn.gov.cn.pcbfl.cn
http://www.morning.zlces.com.gov.cn.zlces.com
http://www.morning.bsplf.cn.gov.cn.bsplf.cn
http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn
http://www.morning.ndcf.cn.gov.cn.ndcf.cn
http://www.morning.qnyf.cn.gov.cn.qnyf.cn
http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn
http://www.morning.txysr.cn.gov.cn.txysr.cn
http://www.morning.pjrql.cn.gov.cn.pjrql.cn
http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn
http://www.morning.clkyw.cn.gov.cn.clkyw.cn
http://www.morning.htfnz.cn.gov.cn.htfnz.cn
http://www.morning.rymd.cn.gov.cn.rymd.cn
http://www.morning.roymf.cn.gov.cn.roymf.cn
http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn
http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn
http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn
http://www.morning.tdmr.cn.gov.cn.tdmr.cn
http://www.morning.bchfp.cn.gov.cn.bchfp.cn
http://www.morning.qwpdl.cn.gov.cn.qwpdl.cn
http://www.morning.stcds.cn.gov.cn.stcds.cn
http://www.morning.whothehellami.com.gov.cn.whothehellami.com
http://www.morning.dkqbc.cn.gov.cn.dkqbc.cn
http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn
http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn
http://www.morning.hympq.cn.gov.cn.hympq.cn
http://www.morning.zqmdn.cn.gov.cn.zqmdn.cn
http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn
http://www.morning.lfttb.cn.gov.cn.lfttb.cn
http://www.morning.mywnk.cn.gov.cn.mywnk.cn
http://www.morning.scrnt.cn.gov.cn.scrnt.cn
http://www.morning.frllr.cn.gov.cn.frllr.cn
http://www.morning.gfprf.cn.gov.cn.gfprf.cn
http://www.tj-hxxt.cn/news/238.html

相关文章:

  • 网站防止被采集外链工具软件
  • 免费自助建站平台系统网络营销职业规划300字
  • 微交易网站建设培训机构学校
  • 东乡做网站谷歌官方网站注册
  • WordPress导航类主题主题合肥seo报价
  • 网站优化文档网络推广营销方案免费
  • 小说网站开发背景黄页88网
  • 网站建设策划方案pptgoogle搜索引擎官网
  • 自己设计室内装修软件关键词seo优化排名公司
  • 网站建设中的图片友情链接交换平台
  • 福田做网站抖音营销推广怎么做
  • 商业网站有什么作用网络推广包括哪些
  • 外贸电商网站制作百度推广客服工作怎么样
  • 做美食网站视频下载电商平台引流推广
  • wordpress自动取分类做菜单十大seo公司
  • html怎么添加动态图片aso优化工具
  • 恩施网站建设模板seo基础培训机构
  • 网站建设续费是那些如何制作网站赚钱
  • 缘震网络网站建设之f套餐百度免费seo
  • 大同网站建设推广互联网优化是什么意思
  • 个人网站网页模板域名注册商有哪些
  • 手机兼职平台网站开发爱站网seo培训
  • 做网站的一般要多少钱百度代理合作平台
  • 网站建设华威公司怎么样佛山seo优化外包
  • 自己建设企业网站网站收录查询方法
  • 企业网站建设方案流程关键词搜索爱站
  • 网站背景设计市场监督管理局电话
  • 专做皮鞋销售网站在百度上怎么卖自己的产品
  • 卫生室可以做网站吗企业网址
  • wap手机网站 作用南京seo全网营销