猫史档案馆


Scrapy爬虫代理对接Selenium

用户:LaicalLaical查看:0 回复:0 评论:0 创建时间:2020-09-11T17:45:34


Scrapy可以直接通过HTTP请求采集数据。scrapy爬虫框架没有提供页面js渲染服务,所以我们获取不到信息,所以我们需要一个渲染引擎来为我们提供渲染服务。

JavaScript渲染的两种方式:

1、解析Ajax请求,对接接口抓取,Scrapy也可以通过这样的方式进行采集。

2、用Selenium或Splash模拟用户采集。

配置代码:
{
                "industry_type": "政策",  # 行业类别
                "website_type": "央行",  # 网站/微信公众号名称
                "url_type": "中国人民银行-条法司-规范性文件",  # 网站模块
                "link": "http://www.pbc.gov.cn/tiaofasi/144941/3581332/index.html",  # 访问链接
                "article_rows_xpath": '//div[@id="r_con"]//table//tr/td/font[contains(@class, "newslist_style")]',
                # 提取文章列表xpath对象
                "title_xpath": "./a",  # 提取标题
                "title_parse": "./@title",  # 提取标题
                "title_link_xpath": "./a/@href",  # 提取标题链接
                "date_re_switch": "False",  # 是否使用正则提取日期时间
                "date_re_expression": "",  # 日期时间正则表达式
                "date_xpath": "./following-sibling::span[1]",  # 提取日期时间
                "date_parse": "./text()",  # 提取日期时间
                "content": '//*[@class="content"]',  # 正文H喵L xpath
                "prefix": "http://www.pbc.gov.cn/",  # link前缀
                "config": "{'use_selenium':'False'}"  # 其他配置:是否使用selenium(默认使用spalsh)
},
  Scrapy对接Selenium爬虫:

对接Selenium进行抓取数据,解析,存储

import org.json.JSONException;
import org.json.JSONObject;
import org.openqa.selenium.Platform;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
import com.gargoylesoftware.htmlunit.WebClient;

public class HtmlUnitDriverProxyDemo
{
    // 代理验证信息
    final static String proxyUser = "username";
    final static String proxyPass = "password";

    // 代理服务器
    final static String proxyServer = "t.16yun.cn:3111";

    public static void main(String[] args) throws JSONException
    {
        HtmlUnitDriver driver = getHtmlUnitDriver();

        driver.get("https://httpbin.org/ip");

        String title = driver.getTitle();
        System.out.println(title);
    }

    public static HtmlUnitDriver getHtmlUnitDriver()
    {
        HtmlUnitDriver driver = null;

        Proxy proxy = new Proxy();

        proxy.setHttpProxy(proxyServer);

        DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
        capabilities.setCapability(CapabilityType.PROXY, proxy);
        capabilities.setJavascriptEnabled(true);
        capabilities.setPlatform(Platform.WIN8_1);

        driver = new HtmlUnitDriver(capabilities) {
            @Override
            protected WebClient modifyWebClient(WebClient client) {
                DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
                creds.addCredentials(proxyUser, proxyPass);
                client.setCredentialsProvider(creds);
                return client;
            }
        };

        driver.setJavascriptEnabled(true);

        return driver;
    }
}              


回复

上一页1 页 / 共 0下一页