手把手教你寫網絡爬蟲(7):URL去重
作者:佚名
本期我們來聊聊URL去重那些事兒。以前我們曾使用Python的字典來保存抓取過的URL,目的是將重復抓取的URL去除,避免多次抓取同一網頁。
本系列:
- 《手把手教你寫網絡爬蟲(1):網易云音樂歌單》
- 《手把手教你寫網絡爬蟲(2):迷你爬蟲架構》
- 《手把手教你寫網絡爬蟲(3):開源爬蟲框架對比》
- 《手把手教你寫網絡爬蟲(4):Scrapy入門》
- 《手把手教你寫網絡爬蟲(5):PhantomJS實戰》
- 《手把手教你寫網絡爬蟲(6):分布式爬蟲》
IPv6編碼地址數:2^128(約3.4×10^38)
IPv6是IETF設計的用于替代現行版本IP協議(IPv4)的下一代IP協議,號稱可以為全世界的每一粒沙子編上一個網址。
- 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()一下,看看是不是已經置為1。
03 index右移6位就是除以64,說明data是long型的數組,除以64就定位到了bit所在的數組下標。1L左移index位,定位到了bit在long中的位置。
責任編輯:龐桂玉
來源:
Python開發者