手把手教你寫網(wǎng)絡(luò)爬蟲(chóng)(7):URL去重
作者:佚名
本期我們來(lái)聊聊URL去重那些事兒。以前我們?cè)褂肞ython的字典來(lái)保存抓取過(guò)的URL,目的是將重復(fù)抓取的URL去除,避免多次抓取同一網(wǎng)頁(yè)。
本系列:
- 《手把手教你寫網(wǎng)絡(luò)爬蟲(chóng)(1):網(wǎng)易云音樂(lè)歌單》
- 《手把手教你寫網(wǎng)絡(luò)爬蟲(chóng)(2):迷你爬蟲(chóng)架構(gòu)》
- 《手把手教你寫網(wǎng)絡(luò)爬蟲(chóng)(3):開(kāi)源爬蟲(chóng)框架對(duì)比》
- 《手把手教你寫網(wǎng)絡(luò)爬蟲(chóng)(4):Scrapy入門》
- 《手把手教你寫網(wǎng)絡(luò)爬蟲(chóng)(5):PhantomJS實(shí)戰(zhàn)》
- 《手把手教你寫網(wǎng)絡(luò)爬蟲(chóng)(6):分布式爬蟲(chóng)》
IPv6編碼地址數(shù):2^128(約3.4×10^38)
IPv6是IETF設(shè)計(jì)的用于替代現(xiàn)行版本IP協(xié)議(IPv4)的下一代IP協(xié)議,號(hào)稱可以為全世界的每一粒沙子編上一個(gè)網(wǎng)址。
- public <T> boolean put(T object, Funnel<? super T> funnel, int numHashFunctions, BitArray bits) {
- long bitSize = bits.bitSize();
- long hash64 = Hashing.murmur3_128().hashObject(object, funnel).asLong();
- int hash1 = (int) hash64;
- int hash2 = (int) (hash64 >>> 32);
- boolean bitsChanged = false;
- for (int i = 1; i <= numHashFunctions; i++) {
- int combinedHash = hash1 + (i * hash2);
- // Flip all the bits if it's negative (guaranteed positive number)
- if (combinedHash < 0) {
- combinedHash = ~combinedHash;
- }
- bitsChanged |= bits.set(combinedHash % bitSize);
- }
- return bitsChanged;
- }
- boolean set(long index) {
- if (!get(index)) {
- data[(int) (index >>> 6)] |= (1L << index);
- bitCount++;
- return true;
- }
- return false;
- }
- boolean get(long index) {
- return (data[(int) (index >>> 6)] & (1L << index)) != 0;
- }
02 先get()一下,看看是不是已經(jīng)置為1。
03 index右移6位就是除以64,說(shuō)明data是long型的數(shù)組,除以64就定位到了bit所在的數(shù)組下標(biāo)。1L左移index位,定位到了bit在long中的位置。
責(zé)任編輯:龐桂玉
來(lái)源:
Python開(kāi)發(fā)者