웹 페이지의 결과는 '추가 매개 변수'에 따라 동일하거나 다를 수 있습니다. 따라서 일반적으로 URL을 보면 중복 콘텐츠를 탐지하는 규칙을 정의 할 수 없습니다.
나는 url1과 url2를 different.Compute로하여 각 블록의 md5sum을 URL로부터받은 1024 단어로 처리하도록 제안합니다. 중복을 탐지 할 수 있도록 이러한 md5sum의 해시 맵을 유지 관리합니다.
아마도일부는 필요한 기능 중 일부를 제공 할 수 있습니다.
OP의 의견을 바탕으로 업데이트 : 내 대답을 향상시키기위한 몇 가지 코드를 작성했습니다. 두 가지 버전이 있습니다 : 첫 번째는 간단하다 : 위의 코드가 예상되는 방법으로 중복을 감지 기대하는 순진했다
def find_matches():
"""
Basic version: reads urls, but does not consider the semantic information of
HTML header, body, etc. while computing duplicates.
"""
from urllib2 import urlopen
import hashlib
urls = [ 'http://www.google.com', 'http://www.google.com/search']
d = {}
url_contents = {}
matches = []
for url in urls:
c = urlopen(url)
url_contents[url] = []
while 1:
r = c.read(4096)
if not r: break
md5 = hashlib.md5(r).hexdigest()
url_contents[url].append(md5)
if md5 in d:
url2 = d[md5]
matches.append((md5, url, url2))
else:
d[md5] = []
d[md5].append(url)
#print url_contents
print matches
if __name__ == '__main__':
find_matches()
이 : 현재 웹 페이지가 너무 복잡하다. 따라서 사용자의 눈에는 똑같은 두 개의 URL도 실제로 광고, 해시 태그, 자체 URL 이름 포함 등으로 인해 많은 차이가 있습니다.
두 번째 버전은보다 정교합니다. 그것은 BeautifulSoup에 기초한 내용의 제한된 의미 론적 분석을 소개합니다 :
def find_matches():
"""
Some consideration of the HTML header, body, etc. while computing duplicates.
"""
from urllib2 import urlopen
import hashlib
from BeautifulSoup import BeautifulSoup
import pprint
urls = [ 'http://www.google.com', 'http://www.google.com/search'] # assuming all distinct urls
def txt_md5(txt):
return hashlib.md5(txt).hexdigest()
MAX_FILE_SIZE = 1024*1024*1024
d = {}
url_contents = {}
matches = []
for url in urls:
try:
c = urlopen(url)
url_contents[url] = []
r = c.read(MAX_FILE_SIZE)
soup = BeautifulSoup(r)
header = soup.find('head').text
body = soup.find('body').text
# More fine-grained content options
# like h1, h2, p, etc., can be included.
# Common CSS tags like page, content, etc.
# can also be included.
for h in [header, body]:
print h
md5 = txt_md5(h)
url_contents[url].append((md5, h))
if md5 in d:
url2 = d[md5]
matches.append((md5, url, url2))
else:
d[md5] = []
d[md5].append(url)
except Exception as e:
print "Exception", e
print '---------------'
#pprint.pprint(url_contents)
print matches
if __name__ == '__main__':
find_matches()
그러나 두 번째 버전 역시 작동하지 않습니다. 이유는 동일합니다. 사실 두 URL의 헤드 텍스트의 차이는 포함 된 해시 값이었고 두 URL의 본문 텍스트 간의 차이는 webhp
문자열이었습니다. difflib.context_diff를 사용하여 차이를 계산했습니다.
웹 페이지를 더 지능적으로 구문 분석하고 더 지능적으로 diff를 계산하는 세 번째 버전을 포함하도록 코드를 향상시킬 수 있습니다. 예를 들어, < 5 % diff (이 비율은 difflib 함수를 사용하여 쉽게 계산할 수 있음)의 텍스트도 복사하여 선언합니다.
안녕하세요. 대답 해 주셔서 감사합니다. "md5sums의 해시 맵을 유지 관리하여 중복을 탐지 할 수 있도록"대한 자세한 팁을 알려주십시오. 사실, 나는 부끄러운 것을 배웠지 만, 현재의 프로젝트에서는 상당히 복잡합니다. – mrblue