js逆向入门

中国土地市场网

1.分析请求过程

在这里插入图片描述
在控制台中获取相应的参数
在这里插入图片描述

2.分析js并修改参数

function stringToHex(str) {
        var val = "";
        for (var i = 0; i < str.length; i++) {
            if (val == "") val = str.charCodeAt(i).toString(16); else val += str.charCodeAt(i).toString(16);
        }
        return val;
    }

function YunSuoAutoJump(text) {
    // var width = screen.width;
    var width = "1920";
    // var height = screen.height;
    var height = "1080";
    if (text == null){
        var screendate = width + "," + height;
    } else {
        var screendate = text;
    }

    // var curlocation = window.location.href;
    // if (-1 == curlocation.indexOf("security_verify_")) {
    //     document.cookie = "srcurl=" + stringToHex(window.location.href) + ";path=/;";
    // }
    var location = "/default.aspx?tabid=226&security_verify_data=" + stringToHex(screendate);
    return location;
}

// 接收传入的参数
var _ = process.argv.splice(2)
console.log(YunSuoAutoJump(_[0]));

构建相应的爬虫代码

# -*- coding: utf-8 -*-
# @Time    : 2019/11/1 9:45
# @Author  :

import os
import re

import requests
from lxml import etree


def generate_signature(value):
    """
    generate _signature parameter
    :param value:share_url id
    :return:signature string
    """
    cwd = os.path.dirname(__file__)
    p = os.popen('cd %s && node landchina.js %s' % (cwd, value))
    return p.readlines()[0]


def landchina():
    # 构建session会话
    s = requests.Session()
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Host': 'www.landchina.com',
        'Pragma': 'no-cache',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
    }
    s.headers = headers

    # 第一次请求
    url = "https://www.landchina.com/default.aspx?tabid=226"
    resp1 = s.get(url=url)

    cookie1 = resp1.headers["Set-Cookie"]
    cookie_re = "(security_session_verify=\w+;)"
    security_session_verify = "".join(re.findall(cookie_re, cookie1))
    security_session_verify_url = generate_signature("https://www.landchina.com/default.aspx?tabid=226").strip()

    s.headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Cookie': security_session_verify + " srcurl=" + security_session_verify_url[
                                                         security_session_verify_url.rfind("=") + 1:],
        'Host': 'www.landchina.com',
        'Pragma': 'no-cache',
        'Referer': 'https://www.landchina.com/default.aspx?tabid=226',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
    }
    var_re = "(security_verify_data.*)"
    var = "".join(re.findall(var_re, generate_signature("")))

    # 第二次请求
    url2 = "https://www.landchina.com/default.aspx?tabid=226&" + var
    resp2 = s.get(url2)
    cookie2 = resp2.headers["Set-Cookie"]
    cookie_re = "(security_session_mid_verify=\w+;)"
    security_session_mid_verify = "".join(re.findall(cookie_re, cookie2))
    s.headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive',
        'Cookie': '%s; %s' % (security_session_verify, security_session_mid_verify),
        'Host': 'www.landchina.com',
        'Pragma': 'no-cache',
        'Referer': url2,
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
    }

    # 第三次请求
    rep = s.get(url=url)
    mytree = etree.HTML(rep.text)

    urls = mytree.xpath('//*[@id="TAB_contentTable"]//tr/td[@class="queryCellBordy"]/a')
    for ur in urls:
        title = "".join(ur.xpath(".//text()"))
        c_url = "https://www.landchina.com/"+"".join(ur.xpath("./@href"))
        data = {
            "title" : title,
            "c_url": c_url,
        }
        print(data)


if __name__ == '__main__':
    landchina()

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cocktail_py

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值