会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132476个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术 15167楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/商城官网项目 15168楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 15169楼
Python 全系列/第五阶段:数据库编程/mysql介绍与环境安装 15170楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC旧 15171楼

问题如下:

我想爬取淘宝的商品信息,但是爬不到,希望老师能检查一下我的代码,希望老师不要敷衍,拜托了

zongzi.py

from selenium import webdriver
import time
import random
from cook import TAO_USERNAME, TAO_PASSWORD
import csv


def search_product(keyword):
    """根据关键字搜索商品,解决登录"""
    # 输入关键字
    driver.find_element_by_xpath('//*[@id="q"]').send_keys(keyword)
    # 设置休息时间,随机休眠1—3秒钟
    time.sleep(random.randint(1, 3))
    # 点击搜索按钮
    driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()

    # 解决登录
    # 输入用户名
    driver.find_element_by_xpath('//*[@id="fm-login-id"]').send_keys(TAO_USERNAME)
    # 设置休息时间,随机休眠1—2秒钟
    time.sleep(random.randint(1, 2))
    # 输入密码
    driver.find_element_by_xpath('//*[@id="fm-login-password"]').send_keys(TAO_PASSWORD)
    # 设置休息时间,随机休眠1—2秒钟
    time.sleep(random.randint(1, 2))
    # 点击登录按钮
    driver.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()

def parse_data():
    """解析数据"""
    # 获取所有想要的div标签
    divs = driver.find_elements_by_xpath('//div[@class="grid g-clearfix"]/div/div')

    # 二次提取
    for div in divs:
        try:
            title = div.find_element_by_xpath('.//div[@class="row row-2 title"]/a').text  # 名字
            price = div.find_element_by_xpath('.//strong').text + '元'  # 价格
            deal = div.find_element_by_xpath('.//div[@class="deal-cnt"]').text  # 付款人数
            name = div.find_element_by_xpath('.//div[@class="shop"]/a/span[2]').text  # 店铺
            location = div.find_element_by_xpath('.//div[@class="location"]').text  # 地址
            detail_url = div.find_element_by_xpath('.//div[@class="pic"]/a').text  # 详情页的URL
            print(title, price, deal, name, location, detail_url)

            # 保存文件
            with open('淘宝.csv', mode='a', encoding='utf-8', newline='') as f:
                csv_write = csv.writer(f) # 实例化csv模块写入对象
                csv_write.writerow([title, price, deal, name, location, detail_url])
        except:
            continue

word = input('请输入你要搜索商品的关键字:')
# 创建一个浏览器
driver = webdriver.Chrome()
# 绕过selenium的检测
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",
            {"source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"""})

# 执行自动化页面操作——输入要爬取得网站
driver.get('https://www.taobao.com/')
# 最大化窗口
driver.maximize_window()
# 设置浏览器的隐式等待
driver.implicitly_wait(10)

# 调用搜索商品的函数
search_product(word)
# 设置休息时间,随机休眠2-4秒钟
time.sleep(random.randint(2, 4))

# 获取前十页的数据
for page in range(100):
    print(f'\n============================正在抓取第{page + 1}数据=====================================')
    driver.get(f'https://www.taobao.com/search?a={word}&s={page * 44}')
# 调用数据解析函数
parse_data()
# 设置休息时间,随机休眠2-4秒钟
time.sleep(random.randint(2, 4))

cook.py

TAO_USERNAME = "用户"
TAO_PASSWORD = "密码"


Python 全系列/第十五阶段:Python 爬虫开发/爬虫反反爬- 15173楼
Python 全系列/第一阶段:Python入门/编程基本概念 15176楼

public class TestShowMsg {

    public static void main(String[] args) {

        ShowMsg sm = new ShowMsg();
        Generic3<Integer> g = new Generic3<>();
        sm.showFlag(g); // null 这里null 是因为这时候Generic3的对象没有任何值为空
        g.setFlag(20); // 为Generic3中的flag赋值20
        sm.showFlag(g); // 20 将Generic3对象g传入
       /*
        Generic3<Number> g2 = new Generic3<>();
        sm.showFlag(g2);
        g.getFlag(50);
        sm.showFlag(g);
        这里都报错的原因是泛型只确定了你要使用的类型或者类型识别,不考虑继承关系,只看类型!!
*/

        System.out.println("=============================================");
        Generic3<Integer> g2 = new Generic3<>();
        sm.showFlag(g2); 
        g2.setFlag(30); 
        sm.showFlag(g2); 
        Generic3<Number> g3 = new Generic3<>();
        sm.showFlag2(g3); // 因为showFlag2(Generic3<?> generic3)的参数泛型是? 任意类型 所以这里不会报错
        g3.setFlag(50);
        sm.showFlag2(g3);


    }
}
class ShowMsg{
    public void showFlag(Generic3<Integer> generic3){  // 这里传入的参数是Generic3对象 泛型类型定义为Integer
        System.out.println(generic3.getFlag());
    }

    // ? 无界通配符
    public void showFlag2(Generic3<?> generic3){  // 不能确定传入的是什么类型时 泛型类型定义为? 任意类型都可以
        System.out.println(generic3.getFlag());
    }
}

class Generic3<T>{
    private T flag;

    public T getFlag() {
        return flag;
    }

    public void setFlag(T flag) {
        this.flag = flag;
    }
}

老师,我这样理解对吗?

然后还有两个问题:

1、为什么传入参数Generic3<T> generic会报错?  我创建的泛型类是Generic3<T>,T不是表示简单的JAVA类么,

为什么传参时不可以使用?

public void showFlag(Generic3<T> generic3){  
    System.out.println(generic3.getFlag());
}

2、T是简单JAVA类 ?是任意类型 那这里用?作为传入参数的泛型,会不会出现超出T表示的范围?

public void showFlag2(Generic3<?> generic3){  
        System.out.println(generic3.getFlag());
    }


JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 15177楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 15178楼
JAVA 全系列/第二十阶段:租房网(Spring Cloud最新架构)/Livegoods第一天 15179楼

老师 请问我一运行就提示Sat Jul 10 19:43:21 CST 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.是什么情况

WARN:不建议在没有服务器身份验证的情况下建立SSL连接。 MySQL 5.5.45+、5.6.26+和5.7.6+要求如果没有设置显式选项,则默认需要建立SSL连接。 为了符合未使用SSL的现有应用程序,verifyServerCertificate属性被设置为“false”。 您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任库。  

package jdbcdemo;


import java.lang.instrument.ClassFileTransformer;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;


public class JDBCtest {

//向department表中添加一条数据

public void insertDepartments(String dep_name,int location_id) {

Connection conn=null;

Statement statement=null;

//驱动注册

try {

Class.forName("com.mysql.jdbc.Driver");

//创建连接

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8", "root", "qwe555123");

String sql="insert into dep values(default,'"+dep_name+"',"+location_id+")";

statement=conn.createStatement();

int flag=statement.executeUpdate(sql);

System.out.println(flag);

} catch (Exception e) {

// TODO: handle exception

}

finally {

if (statement!=null) {

try {

statement.cancel();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if (conn!=null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

public static void main(String[] args) {

// TODO Auto-generated method stub

JDBCtest text=new JDBCtest();

text.insertDepartments("研发部", 8);

}


JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 15180楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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