springboot集成webmagic和selenium,并部署到linux(问题坑)

8 篇文章 0 订阅
8 篇文章 0 订阅

首先参考两个源代码

黄亿华 / webmagic
NAMEniubi / webmagic-selenium

通过以上源码可以基本了解怎么使用

spring boot集成

将Pipeline、PageProcessor、Spider进行拆分为不同类,拆分后添加@Component使其交给spring管理,使用时进行@Autowired注入

找不到org.openqa.selenium.remote.AbstractDriverOptions的类文件

集成后有时会出现找不到类的情况,是因为spring boot自带<selenium.version>版本号太老导致,需要手动添加maven依赖,这里我是用的是4.8.3版本,如下所示:(缺少哪个添加哪个即可)
在这里插入图片描述
在这里插入图片描述

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>4.8.3</version>
        </dependency>

代理ip–更换一个网页同时更换一个代理ip

添加代理Ip池,说实话怎么添加ip在网上找到了,但是怎么实现上述目标没有找到,所以只好使用笨方法—在更换网页后手动更改当前系统代理

            System.setProperty("proxyType", "4");
            System.setProperty("proxyHost", ip);
            System.setProperty("proxyPort", 端口);
            System.setProperty("proxySet", "true");

有时候需要将代理取消,换回原有代理,执行清理系统代理

        System.clearProperty("proxyHost");
        System.clearProperty("proxyPort");
        System.clearProperty("proxyType");
        System.clearProperty("proxyType");

放置位置在==webDriver.get(request.getUrl());==之前即可

代理ip网址

当前我在用的两个免费代理网站(不过有条件的话还是建议买代理服务,免费的。。。你们懂的)
快代理
66免费代理

部署linux

谷歌浏览器

  • 配置 yum 源

在 /etc/yum.repos.d/ 目录下新建文件 google-chrome.repo

vi /etc/yum.repos.d/google-chrome.repo

写入以下内容

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub    
  • 安装 google chrome 浏览器
 yum -y install google-chrome-stable

Google 官方源可能在中国无法使用,导致安装失败或者在国内无法更新,可以添加以下参数来安装

yum -y install google-chrome-stable --nogpgcheck
  • 位置
    安装后位置在 /usr/bin/google-chrome

下载谷歌浏览器驱动网址(注意和浏览器的版本一致性)

chromedriver
下载linux版本,上传到服务器并解压,复制到/usr/bin下

unzip chromedriver_linux64.zip
cp chromedriver /usr/bin

在这里插入图片描述
在这里插入图片描述

查看版本

google-chrome --version
chromedriver --version

在这里插入图片描述

验证代理Ip是否可用

    //无效的ip 返回true 有效的ip返回false
    static boolean ifUselessBaidu(String ip, String port) {
        String[] split = ip.split(":");
        URL url = null;
        try {`在这里插入代码片`
            url = new URL("http://www.baidu.com");
            InetSocketAddress addr = new InetSocketAddress(ip, Integer.parseInt(port));
            Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
            InputStream in = null;
            try {
                URLConnection conn = url.openConnection(proxy);
                conn.setConnectTimeout(2000);
                in = conn.getInputStream();
            } catch (Exception e) {
                return true;
            }
            String s = IOUtils.toString(in);
            if (s.indexOf("baidu") > 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return true;
        }
    }

    static List<String> arrs = new ArrayList<String>() {{
        add("http://ip.3322.net/");
        add("http://icanhazip.com/");
    }};
//无效的ip 返回true 有效的ip返回false
    static boolean ifUseless(String ip, String port) {
        URL url = null;
        try {
            url = new URL(arrs.get(new Random().nextInt(arrs.size())));
            InetSocketAddress addr = new InetSocketAddress(ip, Integer.parseInt(port));
            Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
            InputStream in = null;
            try {
                URLConnection conn = url.openConnection(proxy);
                conn.setConnectTimeout(2000);
                in = conn.getInputStream();
            } catch (Exception e) {
                return true;
            }
            String resultIp = IOUtils.toString(in);
//            System.out.println(ip + "----" + resultIp);
            if (resultIp.trim().equals(ip)) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            return true;
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值