2017-10-05 8 views
-2

나는 치료를 위해 일반 스크레이퍼를 구성하려고 시도했다. 아이디어는 URL을 입력으로 받아 해당 URL의 페이지 만 긁어 내야하지만, YouTube 등에서 사이트를 벗어나는 것처럼 보입니다. 이상적으로는 1,2를 허용하는 깊이 옵션도 있습니다. , 3, 등등. 이것을 달성하는 방법에 대한 아이디어가 있습니까?치료 일반 스크레이퍼

from bs4 import BeautifulSoup 
from bs4.element import Comment 
import urllib 
from route import urls 
import pickle 
import os 
import urllib2 
import urlparse 

def tag_visible(element): 
    if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']: 
     return False 
    if isinstance(element, Comment): 
     return False 
    return True 


def text_from_html(body): 
    soup = BeautifulSoup(body, 'html.parser') 
    texts = soup.findAll(text=True) 
    visible_texts = filter(tag_visible, texts) 
    return u" ".join(t.strip() for t in visible_texts) 

def getAllUrl(url): 
    try: 
     page = urllib2.urlopen(url).read() 
    except: 
     return [] 
    urlList = [] 
    try: 
     soup = BeautifulSoup(page) 
     soup.prettify() 
     for anchor in soup.findAll('a', href=True): 
      if not 'http://' in anchor['href']: 
       if urlparse.urljoin(url, anchor['href']) not in urlList: 
        urlList.append(urlparse.urljoin(url, anchor['href'])) 
      else: 
       if anchor['href'] not in urlList: 
        urlList.append(anchor['href']) 

     length = len(urlList) 

     return urlList 
    except urllib2.HTTPError, e: 
     print e 

def listAllUrl(url): 
    urls_new = list(set(url)) 
    return urls_new 
count = 0 

main_url = str(raw_input('Enter the url : ')) 
url_split=main_url.split('.',1) 
folder_name =url_split[1] 
txtfile_split = folder_name.split('.',1) 
txtfile_name = txtfile_split[0] 
url = getAllUrl(main_url) 
urls_new = listAllUrl(url) 

os.makedirs('c:/Scrapy/Extracted/'+folder_name+"/") 
for url in urls_new: 
    if url.startswith("http") or url.startswith(" "): 
     if(main_url == url): 
      url = url 
     else: 
      pass 
    else: 
     url = main_url+url 
    if '#' in url: 
     new_url = str(url).replace('#','/') 
    else: 
     new_url =url 
    count = count+1 
    if new_url: 
     print""+str(count)+">>",new_url 
     html = urllib.urlopen(new_url).read() 
     page_text_data=text_from_html(html) 
     with open("c:/Scrapy/Extracted/"+folder_name+"/"+txtfile_name+".txt", "a") as myfile: 
      myfile.writelines("\n\n"+new_url.encode('utf-8')+"\n\n"+page_text_data.encode('utf-8')) 
      path ='c:/Scrapy/Extracted/'+folder_name+"/" 
     filename ="url"+str(count)+".txt" 
     with open(os.path.join(path, filename), 'wb') as temp_file: 
      temp_file.write(page_text_data.encode('utf-8')) 
      temp_file.close() 
    else: 
     pass  

답변

1

현재 솔루션에는 치료가 전혀 필요하지 않습니다. 그러나 당신이 특별히 치료에 대해 물었을 때, 여기 있습니다.

거미는 CrawlSpider 클래스에 기초를두고 있습니다. 이렇게하면 주어진 웹 사이트를 크롤링하고 탐색이 준수해야하는 규칙을 지정할 수 있습니다.

오프 사이트 요청을 금지하려면 allowed_domains 스파이더 속성을 사용하십시오. 또는 CrawlSpider 클래스를 사용하는 경우 LinkExtractor 생성자의 allow_domains (또는 그 반대로, deny_domains) 특성을 Rule에 지정할 수 있습니다.

크롤링 깊이를 제한하려면 settings.pyDEPTH_LIMIT을 사용하십시오.

0

태그 스큐 레이션이 있지만 전혀 사용하지 않습니다. 나는 그것을 사용하려고 시도하는 것이 좋습니다 - 정말 쉽습니다. 스스로 개발하려고하는 것보다 훨씬 쉽습니다. 이미 특정 도메인별로 요청을 제한 할 수있는 옵션이 있습니다.