前端跨域服务使用爬虫解决方案

如果您需要源码地址:gitee
代理跨域的代码 已经配置完成。测试地址:https://api.hzbiz.net/

前端Vue部分代码

<template>
<div class="container">
    <b-jumbotron>
        <template v-slot:header>前端跨域免费代理服务</template>

        <template v-slot:lead>
            如果您需要代理服务地址,请自行查看<a href="https://gitee.com/yyx219/agent" target="_blank">源码</a>
        </template>

        <hr class="my-4">
        <b-form @submit.prevent="onSubmit" v-if="form.show">
            <b-form-group id="input-group-1" label="请求地址:" label-for="input-1" description="请输入要跨域的网址">
                <b-form-input id="input-1" v-model="form.url" type="url" required placeholder="请输入要访问的域名地址"></b-form-input>
            </b-form-group>
            <b-form-group label="请选择您要获取的目标页的内容">
                <b-form-radio-group id="radio-group-2" v-model="form.type" name="radio-sub-component">
                    <b-form-radio value="all">默认请求方式</b-form-radio>
                    <b-form-radio value="links">获取页面所有链接</b-form-radio>
                    <b-form-radio value="text">获取页面所有文本内容</b-form-radio>
                    <b-form-radio value="form">获取表单内容</b-form-radio>
                </b-form-radio-group>
            </b-form-group>

            <b-button type="submit" variant="primary">请求跨域</b-button>
        </b-form>

        <b-card header="请求的结果" v-if="result">
            <b-card-text>
                <pre class="line-numbers language-html">
                {{ result }}
                </pre>
            </b-card-text>
        </b-card>

    </b-jumbotron>
</div>
</template>

<script>
export default {
    name: 'Home',
    data() {
        return {
            form: {
                type: "all",
                url: "",
                show: true
            },
            result: null
        }
    },
    methods: {
        onSubmit() {
            this.axios.get('api/index.php?url=' + this.form.url +
                    '&type=' + this.form.type)
                .then((res) => {
                    this.result = res.data
                    console.log(res)
                })
        }

    }
}
</script>

<style lang="scss">
.container {
    margin-top: 2%;

    .card-header {
        margin-top: 15px;
    }
}
</style>

后端PHP部分代码:

// header("Content-Type:application/json;charset=UTF-8");
header("Access-Control-Allow-Origin:*");
date_default_timezone_set("PRC");
include("Snoopy.class.php");//使用时不要忘记引入Snoopy类库
function checkUrl($url)
{
    $pattern = "/^(http|https):\/\/.*$/i";
    if (preg_match($pattern, $url)) {
        return true;
    } else {
        return false;
    }
}

function GetData($url, $type)
{
    $ch = new Snoopy;
    $ch->agent = "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-CN; Redmi 6 Pro Build/OPM1.171019.019) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.108 UCBrowser/12.5.5.1035 Mobile Safari/537.36";
    $ch->referer = "https://www.hzbiz.net/";
    switch ($type) {
        case "text":
            $ch->fetchtext($url);//获取网页数据的全部文本数据
            break;
        case "links":
            $ch->fetchlinks($url);//获取网页数据的全部链接
            break;
        case "form":
            $ch->fetchform($url); //获取表单
            break;
        default:
            $ch->fetch($url);
    }
    return $ch->results;//返回数据
}


if (isset($_GET['url']) && checkUrl($_GET['url'])) {
    $url = $_GET['url'];
    $type = '';
    if (isset($_GET['type'])) {
        $type = $_GET['type'];
    }
    if($type=='links'){
        echo json_encode(Array("isRule" => true, "message" => GetData($url,$type)));
    }else{
        echo GetData($url,$type);
    }

} else {
    echo json_encode(Array("isRule" => false, "message" => "请输入正确的网址"));
}
暂无评论

请到【后台 - 用户 - 我的个人资料】中填写个人说明。

发表评论