溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

js混淆爬蟲天氣網(wǎng)的方法是什么

發(fā)布時(shí)間:2021-12-29 10:09:06 來(lái)源:億速云 閱讀:159 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“js混淆爬蟲天氣網(wǎng)的方法是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

混淆加密網(wǎng)站

 今天在爬取污染物時(shí)遇到下面網(wǎng)站,總的來(lái)說(shuō)碰到了兩大方面的爬蟲難題。(混淆加密和debug檢測(cè))。

待爬取的網(wǎng)站 js混淆爬蟲天氣網(wǎng)的方法是什么

數(shù)據(jù)獲取

  1. 一開始就遇到右鍵禁用,debug檢測(cè)。 js混淆爬蟲天氣網(wǎng)的方法是什么

    沒(méi)辦法,ctrl+s,直接把網(wǎng)站保存到了本地。

  2. 這樣可以f12了,找到主頁(yè)面,一通找,找到了ajax請(qǐng)求的代碼。 js混淆爬蟲天氣網(wǎng)的方法是什么

    傳入城市、月份,然后調(diào)js中的方法,接著ctrl+shift+f,全局搜索這玩意。 js混淆爬蟲天氣網(wǎng)的方法是什么

  3. 復(fù)制到本地一看,好么,js混淆,找了個(gè)反混淆js。 js混淆爬蟲天氣網(wǎng)的方法是什么

  4. 復(fù)制到本地,一通ctrl+c,終于找到下列代碼,一看邏輯,先加密傳入的參數(shù),在post請(qǐng)求,獲取加密后的結(jié)果,在解密結(jié)果。 js混淆爬蟲天氣網(wǎng)的方法是什么

  5. 理清思路,終于可以寫代碼了。

  6. 注意一下,每個(gè)人的加密參數(shù)(或者隔一段時(shí)間)不一樣,所以大家復(fù)制我的不一定后面能跑,自己可以使用這套路獲取自己的js。 js混淆爬蟲天氣網(wǎng)的方法是什么

python代碼

# -*- coding: utf-8 -*-
import execjs
import json
import requests
import datetime


class pollutionSpider:
    """
    爬取https://www.aqistudy.cn/historydata/daydata.php 污染物數(shù)據(jù)
    """

    def __init__(self):
        self.js_path = "../data/aqistudy.js"
        self.main_url = 'https://www.aqistudy.cn/historydata/api/historyapi.php'
        self.month_data = {"1": "01", "2": "02", "3": "03", "4": "04", "5": "05", "6": "06", "7": "07", "8": "08",
                           "9": "09", "10": "10", "11": "11", "12": "12"}
        self.save_path = "../data/weather/"
        self.headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.8',
            'Content-Type': 'application/x-www-form-urlencoded',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/71.0.3578.80 Safari/537.36 '
        }
        self.data_headers = "time_point	aqi	pm2_5	pm10	so2	no2	co	o3	rank	quality"

    def encrypt(self, city, month):
        """
        加密信息
        """
        js_str = self.get_js()
        ctx = execjs.compile(js_str)  # 加載JS文件
        return ctx.call('pLoXOmdsuMMq', "GETDAYDATA", {"city": city, "month": month})

    def decrypt(self, data):
        """
        解密信息
        """
        ctx = execjs.compile(self.get_js())  # 加載JS文件
        return ctx.call('dSMkMq14l49Opc37Yx', data)

    def get_js(self):
        """
        獲取js
        """
        f = open(self.js_path, 'r', encoding='utf-8')  # 打開JS文件
        line = f.readline()
        html_str = ''
        while line:
            html_str = html_str + line
            line = f.readline()
        return html_str

    def get_response(self, params):
        """
        請(qǐng)求數(shù)據(jù)
        """
        return requests.post(self.main_url, data={'hzbDyDmL0': params}, headers=self.headers).text

    def get_single(self, city, month):
        """
        獲取一個(gè)城市某個(gè)月的數(shù)據(jù)
        """
        encrypt_data = self.get_response(self.encrypt(city, month))
        data = json.loads(self.decrypt(encrypt_data))['result']['data']['items']
        result = ['\t'.join([str(value) for key, value in element.items()]) for element in data]
        return result

    def get_all(self, city, start_day):
        """
        獲取一個(gè)城市污染數(shù)據(jù)
        """
        print("開始獲取" + city + "數(shù)據(jù)------------------------")
        start_day = datetime.datetime.strptime(start_day, "%Y-%m-%d")
        end_day = datetime.datetime.now()
        months = (end_day.year - start_day.year) * 12 + end_day.month - start_day.month
        month_range = ['%s%s' % (start_day.year + mon // 12, self.month_data[str(mon % 12 + 1)]) for mon in
                       range(start_day.month - 1, start_day.month + months)]
        f = open(self.save_path + city + ".txt", "w", encoding="utf8")
        f.write(self.data_headers + "\n")
        for element in month_range:
            try:
                data = self.get_single(city, element)
                for line in data:
                    f.write(line + "\n")
                print(element + city + "數(shù)據(jù)獲取------------------------成功")
            except Exception as e:
                print(e)
                print(element + city + "數(shù)據(jù)獲取------------------------失敗")
        f.close()


if __name__ == '__main__':
    pollutionSpider().get_all("上海", "2015-1-1")

結(jié)果

js混淆爬蟲天氣網(wǎng)的方法是什么

“js混淆爬蟲天氣網(wǎng)的方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI