从URL获取文件保存到本地的代码

网络思维-网络爬虫实训(一) 任务描述本关任务:本关我们将使用Python程序,实现通过网页的url,获得服务器返回的超文本文件,并保存为文本文件。相关知识为了完成本关任务,你需要掌握:1.requests基本操作,2.文本文件操作。requests基本操作requests就是一个基于网络请求的模块,可以用来模拟浏览器发请求。使用方法:参数说明:url :需要抓取的 URL 地址;params : 网址带参请求的方法,字典类型;headers : 请求头。 阅读详情
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<%@page import="java.net.*,java.io.*"%>

  <%!

   public boolean saveUrlAs(String photoUrl, String fileName) {

  //此方法只能用户HTTP协议

   try {

   URL url = new URL(photoUrl);

   HttpURLConnection connection = (HttpURLConnection) url.openConnection();

   DataInputStream in = new DataInputStream(connection.getInputStream());

   DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));

   byte[] buffer = new byte[4096];

   int count = 0;

   while ((count = in.read(buffer)) > 0) {

   out.write(buffer, 0, count);

   }

   out.close();

   in.close();

   return true;

   }

   catch (Exception e) {

   return false;

   }

   }   

  public String getDocumentAt(String urlString) {

  //此方法兼容HTTP和FTP协议

   StringBuffer document = new StringBuffer();

   try {

   URL url = new URL(urlString);

   URLConnection conn = url.openConnection();

   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.

   getInputStream()));

   String line = null;

   while ( (line = reader.readLine()) != null) {

   document.append(line + "/n");

   }

   reader.close();

   }

   catch (MalformedURLException e) {

   System.out.println("Unable to connect to URL: " + urlString);

   }

   catch (IOException e) {

   System.out.println("IOException when connecting to URL: " + urlString);

   }

   return document.toString();

   }

  %>

  <%

  //测试

   String photoUrl = "http://ad4.sina.com.cn/200601/12/43932_750450.jpg";

   String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));

   String filePath = "d:/ghost/";

   boolean flag = saveUrlAs(photoUrl, filePath + fileName);

   out.println("Run ok!/n
Get URL file " + flag);

  %>
爬虫实战——网页抓取及信息提取 当我们想要在浏览器中打开一个网页时,需要在浏览器的地址栏中输入该网页的url,例如在地址栏中输入百度搜索网站的首页url:https://www.baidu.com/ ,点击确认后,浏览器将向服务器发出一个对该网的请求;服务器端收到请求后,会返回该网页的超文本文件,浏览器收到服务器端发来的网页超文本文件后,对其进行解析,然后在窗口中显示该超文本文件对应的网页。通过上一关卡,我们学会了如何对文件中的数据进行分析,现已将2016年国防科技大学录取的分数最高的3个省份,存储至本地文件中。 阅读详情

相关推荐

网页抓取+表单提取源代码

该程序用linux下c写的,主要功能就是一个网页抓取,然后根据抓取的网页提取里面的表单id,name,type等信息,并按form分开,后面功能有需要的可以自己加,都有注释

Python文件 基本操作概况

这里写自定义目录标题Python爬虫入门 文件基本操作概况IO编程文件读取文件的写入如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 Python爬虫入门 文件基本操作概况 由于最近在入门爬虫,...

RichardLau_Cx的博客 506

VCSA 6.7证书更新全流程指南:从故障诊断到完整替换

本文详细介绍了VCSA 6.7证书更新的全流程,包括故障诊断、证书替换操作步骤及常见问题解决方案。通过使用官方脚本和证书管理器,帮助管理员快速解决证书过期问题,确保vCenter系统的稳定运行。特别强调了STS证书修复和全量证书重置的关键步骤,适用于vcsa6.7环境下的证书管理需求。

weixin_42634811的博客 450

网页爬取综合实例

利用urllib.request模块中的方法,补全step1()函数。仔细阅读网页源代码信息,补全step2()函数。使用find()函数定位这五个url,并保存在列表urls中(顺序从2021-2014倒序)。正确使用urllib.request的相关函数获取指定url的内容;注意:提取的超链是相对地址,需要加上站点域名,拼接成完整的URL。将获取的页面内容,写入本地文件,命名为nudt.txt。

2301_77225918的博客 2587

使用 ArxivLoader 从 arXiv 加载学术文献并实现高效文档处理

arXiv是一个开放获取的学术存储平台,涵盖了物理、数学、计算机科学等多个领域的 200 万篇学术论文。这些文献中包含了如逻辑推理、机器学习等热门领域的最新研究进展。通过使用 ArxivLoader,我们可以轻松地搜索和加载 arXiv 上的学术文献,并将其转换为文本以供分析或应用于下游任务,例如问答系统、摘要生成或知识图谱构建。有时我们并不需要完整的文献内容,而是只需要摘要用于快速预览。可以使用 get_summaries_as_docs()# 获取文献摘要# 查看第一篇摘要。

hgSdaegva的博客 888

Windows10完美驱动Apple Studio Display全攻略:从雷电转接到亮度调节(附N卡兼容列表)

本文详细介绍了在Windows10系统下如何完美驱动Apple Studio Display显示器,包括雷电接口转接方案、BootCamp驱动安装及NVIDIA显卡兼容性测试。特别推荐GTX 1660 Super等显卡,提供5K@60Hz稳定输出,并分享色彩管理及日常使用技巧,帮助用户充分发挥这款苹果显示器的性能。

weixin_29284657的博客 1088

从URL获取文件保存到本地的代码 [轉貼]

源地址:http://blog.youkuaiyun.com/54powerman/archive/2006/04/03/649069.aspx%@page import="java.net.*,java.io.*"%>  public boolean saveUrlAs(String photoUrl, String fileName) {//此方法只能用户HTTP协议    try {     

冯立彬的博客 7202

Claude Code长期记忆:优雅的上下文管理

ClaudeCode的上下文管理系统采用三层架构设计,通过智能压缩和优化策略在记忆容量与性能间取得平衡。系统包含短期记忆(高速工作区)、中期记忆(智能蒸馏器)和长期记忆(持久化知识库)。当Token使用率达到92%阈值时启动wU2压缩器,采用8段式结构化摘要保留关键信息,并配合智能文件恢复和渐进式预警机制。该系统通过分层处理、优雅降级和透明化交互,为AI应用提供了高效可靠的上下文管理方案,展现了在有限资源下实现智能记忆的艺术。

m0_53117338的博客 3816

java url 获取文件_[转]从URL获取文件保存到本地的JAVA代码

public boolean saveUrlAs(String photoUrl, String fileName) {//此方法只能用户HTTP协议try {URL url = new URL(photoUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();DataInputStream in =...

weixin_42126749的博客 337

折叠式共源共栅放大器设计/备忘

w折叠式共源共栅基本结构如图

Czy1377004611的博客 3万+

Pdu Router(PduR)模块

1. PduR路由基本功能 2. PduR总线网关功能 3. PduR缓存机制 4. 总结

weixin_43759043的博客 3849

java 读取url文件下载到本地_从URL获取文件保存到本地的JAVA代码

public boolean saveUrlAs(String photoUrl, String fileName) {//此方法只能用户HTTP协议try {URL url = new URL(photoUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();DataInputStream in =...

weixin_35562134的博客 319

Simulink开发项1000例实战专栏--实例64:基于Simulink构建一个储能系统模型

通过引入储能系统,太阳能电站能够实现在白天储存多余的能量,在夜晚释放能量,平滑负荷曲线,提高电网的稳定性和可靠性。本项目的目标是基于Simulink平台,构建一个完整的储能系统模型,并通过仿真验证其在电网调峰中的应用效果。通过引入储能系统,工业园区能够实现在负荷高峰期释放能量,在负荷低谷期储存能量,平滑负荷曲线,降低用电成本,提高电网的稳定性和可靠性。通过引入储能系统,风力发电场能够实现在负荷高峰期释放能量,在负荷低谷期储存能量,平滑负荷曲线,提高电网的稳定性和可靠性。

xiaoheshang_123的博客 515

从URL获取文件保存到本地的JAVA代码

<%! public boolean saveUrlAs(String photoUrl, String fileName) { //此方法只能用户HTTP协议 try { URL url = new URL(photoUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnect

幸福的使者 5131

微信小程序 - 实现身份证OCR识别人像面与国徽面并提取字段信息,详解从手机相册选取或打开摄像头拍照上传身份证正反面图片ocr文字识别数据填充表单(提供免费纯前端ocr识别插件+百度OCR识别API)

微信小程序身份证识别,微信小程序身份证拍照、上传并OCR识别,微信小程序实现OCR扫描识别,微信小程序识别身份证免费插件,微信小程序实现身份证识别功能。ocr识别插件。微信身份证拍照后进行ocr识别,微信小程序OCR扫描识别身份证,OCR身份证实名认证+图片上传+拍照上传图片,微信小程序之身份证照片正反上传,小程序OCR身份证识别,微信小程序实现文字识别-ocr插件,微信小程序利用百度AI实现扫描身份证获取信息功能,基于微信小程序OCR插件实现身份证拍照、上传并OCR识别的示例demo,将上传的照片传到服务

🏆 前端领域优质创作者 8133

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

java url 获取文件_[转]从URL获取文件保存到本地的JAVA代码,url 请求设置http请求头

java url 获取文件_[转]从URL获取文件保存到本地的JAVA代码,url 请求设置http请求头 public boolean saveUrlAs(String photoUrl, String fileName) { //此方法只能用户HTTP协议 try { URL url = new URL(photoUrl); HttpURLConnection connection = (HttpURLConnection) u.

万事俱备,就差一个程序员了 268

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

(转)从URL获取文件保存到本地的JAVA代码

[code="java"] 0) { out.write(buffer, 0, count); } out.close(); in.close(); return true; } catch (Exception e) { return false; } } ...

iteye_3380的博客 351

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

上一篇: ASP显示目录下所有的文件代码
下一篇: CSS+DIV+JS树形2层菜单
piperzero
博客等级 码龄21年 1377粉丝 504原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值