会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132440个问题
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 19306楼
JAVA 全系列/第五阶段:JavaWeb开发/Ajax技术详解(旧) 19307楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 19308楼
Python 全系列/第一阶段:Python入门/编程基本概念 19309楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 19310楼

老师,下面这个代码是为了下载搜狐登录界面的验证码图片,但是我换了几种方式发送请求,打印resp.text都是得到:

{"body":"","message":"Bad Request","status":400},但是打印的验证码图片链接又可以访问,这是为啥呢?

具体代码如下:

import execjs
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36',
    'Referer': 'https://sohu.com/'}

# 获得验证码链接中的pagetoken和random参数
def get_form_data():
    js_callback = """var randomName = function(prefix) {
                nonce = (new Date).getTime();
                return "passport403" + "_" + prefix + nonce++
            }"""

    ctx = execjs.compile(js_callback)
   
    random_ = ctx.call('randomName', 'sdk')

    js_pagetoken = """
        var pagetoken = (new Date).getTime()+1
        return pagetoken
    """
    ctx_page = execjs.compile(js_pagetoken)
    pagetoken = str(ctx_page.call(js_pagetoken))
    return (pagetoken, random_)


pagetoken, random_ = get_form_data()

url = f"https://v4.passport.sohu.com/i/captcha/picture?pagetoken={pagetoken}&random={random_}"
# 发送请求
resp = requests.get(url,headers=headers)
print(resp.text)
# {"body":"","message":"Bad Request","status":400}
print(url)


Python 全系列/第十五阶段:Python 爬虫开发/动态数据抓取 19311楼
JAVA 全系列/第四阶段:网页编程和设计/CSS3(旧) 19312楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 19314楼
Python 全系列/第五阶段:数据库编程/python操作mysql(旧) 19315楼

老师好,我运行了之后客户端收到的信息是乱码,这个怎么解决呢?

客户端代码:

package com.bjsxt.client;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Test {
    public static void main(String[] args) throws IOException {
        //(1)创建Socket对象
        Socket client = new Socket("127.0.0.1", 9999);
        //(2)获取输出流
        OutputStream os = client.getOutputStream();
        os.write('a');
        //(3)获取输入流
        InputStream is = client.getInputStream();
        byte [] buf = new byte[1024];
        int len=0;//读到的字节的个数
        while ((len=is.read())!= -1){
            System.out.println(new String(buf,0,len));
        }
        //(4)关闭流
        if (is!=null){
            is.close();
        }
        if(os!=null){
            os.close();
        }
        if (client!=null){
            client.close();
        }
    }
}

服务端代码:

package com.bjsxt.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("----------服务器端已启动-----------");
        //(1)创建ServerSocket对象

        ServerSocket server = new ServerSocket(9999);

        //(2)监听是否有客户端来请求连接
        Socket client = server.accept();
        //(3)获取输入流
        InputStream is = client.getInputStream();
        System.out.println((char) is.read());

        //(4)获取输出流
        OutputStream os = client.getOutputStream();
        os.write("收到了!".getBytes());
        //(5)关闭流,关闭Socket
        if (os!=null){
            os.close();
        }
        if (is!=null){
            is.close();
        }
        if (client!=null){
            client.close();
        }
    }
}

运行结果:

image.png

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 19316楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 19317楼

pygame
SCREEN_WIDTH=SCREEN_HEIGHT=BG_COLOR=pygame.Color()  MainGame():
    window=():
        ():
         pygame.display.init()
        MainGame.window=pygame.display.set_mode([SCREEN_WIDTHSCREEN_HEIGHT])
        pygame.display.set_caption()
        :
            MainGame.window.fill(BG_COLOR)
            .getEvent()
            pygame.display.update()

    ():
        ()
        ()
    ():
        pygame.font.init()
        (pygame.font.get_fonts())
        ():
        eventList=pygame.event.get()
        event eventList:
            event.type == pygame.QUIT:
                .endGame()
            event.type == pygame.KEYDOWN:
                event.key == pygame.K_LEFT:
                   ()
                event.key == pygame.K_RIGHT:
                    ()
                event.key == pygame.K_UP:
                    ()
                event.key == pygame.K_DOWN:
                    ()


Tank():
    ():
        ():
        ():
        ():
        Mytank(Tank):
    ():
        Enemytank(Tank):
    ():
        Bullet():
    ():
        ():
        ():
        Wall():
    ():
        ():
        Music():
    ():
        ():
        Explode():
    ():
        ():
        __name__==:
    MainGame().getTextSuface()


Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 19318楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 19319楼

package com.bjsxt.copy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestCopy {
	public static void main(String[] args) {
		/*File srcFile=new File("D:\\180416\\五一作业.docx");
		File targetFile=new File("E:\\五一作业.docx");
		//调用复制文件的方法
		copyFile(srcFile, targetFile);*/
		File srcDir=new File("D:\\180416");
		File targetDir=new File("E:\\180416");
		//调用复制指定目录下所有文件的方法
		copyDir(srcDir, targetDir);
	}
	public static void copyDir(File srcDir,File targetDir){
		if(!targetDir.exists()){
			targetDir.mkdir();//如果目的地的目录不存在,则需要使用File类的方法进行创建目录
		}
		File []files=srcDir.listFiles(); //获取指定目录下的所有File对象
		for (File file : files) {
			if (file.isFile()) {
				 //复制  srcDir -->D:\\180416  拼接  D:\\180416\\XXXX.doc文件
				//    targetDir-->E:\\180416 拼接 E:\\180416\\XXXX.doc文件
				copyFile(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}else{
				//继续调用该方法,使用递归实现
				copyDir(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}
		}
	}
	public static void copyFile(File srcFile,File targetFile){
		//(1)提高读取效率,从数据源
		BufferedInputStream bis=null;
		//(2)提高写入效率,写到目的地
		BufferedOutputStream bos=null;
		try {
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			
			bos = new BufferedOutputStream(new FileOutputStream(targetFile));
			//(3)边读边写
			byte [] buf=new byte[1024];//中转站
			int len=0;//用于存储读到的字节的个数
			while((len=bis.read(buf))!=-1){
				bos.write(buf,0,len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		//(4)关闭
			if(bos!=null){
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(bis!=null){
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}
	}
}

这个方法中拼接文件名字部分代码不太理解

public static void copyDir(File srcDir,File targetDir){
		if(!targetDir.exists()){
			targetDir.mkdir();//如果目的地的目录不存在,则需要使用File类的方法进行创建目录
		}
		File []files=srcDir.listFiles(); //获取指定目录下的所有File对象
		for (File file : files) {
			if (file.isFile()) {
				 //复制  srcDir -->D:\\180416  拼接  D:\\180416\\XXXX.doc文件
				//    targetDir-->E:\\180416 拼接 E:\\180416\\XXXX.doc文件
				copyFile(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}else{
				//继续调用该方法,使用递归实现
				copyDir(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
			}
		}
	}

要传入copyFile方法的参数是文件 不是只能传文件吗 又传入"\\"是干什么的  还有file.getName 完全不懂

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 19320楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637