2017-01-04 4 views
1

Webクローラを作成しています。私はスクラピーや何かを使用していない、私は私のスクリプトがほとんどのことをやろうとしている。問題の検索を試みましたが、エラーの原因となるものを見つけることができませんでした。私は問題の絞り込みを試みるために変数のいくつかを切り替えようとしました。 24行目で、IndexError:文字列インデックスが範囲外です。というエラーが表示されます。関数は最初のurl(元のurl)で実行され、次に2番目のurlで実行され、元の配列で3番目の関数で失敗します。私は迷っています、どんな助けも大いに評価されるでしょう!注:私はテストのためだけにすべてを印刷しています。私は最終的にそれらをテキストファイルに印刷します。 a_string = str(a)試して追加した後Webクローラに関する問題:IndexError:文字列インデックスが範囲外になっています

import requests 
from bs4 import BeautifulSoup 

# creating requests from user input 
url = raw_input("Please enter a domain to crawl, without the 'http://www' part : ") 

def makeRequest(url): 
    r = requests.get('http://' + url) 
    # Adding in BS4 for finding a tags in HTML 
    soup = BeautifulSoup(r.content, 'html.parser') 
    # Writes a as the link found in the href 
    output = soup.find_all('a') 
    return output 


def makeFilter(link): 
    # Creating array for our links 
    found_link = [] 
    for a in link: 
     a = a.get('href') 
     a_string = str(a) 

     # if statement to filter our links 
     if a_string[0] == '/': # this is the line with the error 
      # Realtive Links 
      found_link.append(a_string) 

     if 'http://' + url in a_string: 
      # Links from the same site 
      found_link.append(a_string) 

     if 'https://' + url in a_string: 
      # Links from the same site with SSL 
      found_link.append(a_string) 

     if 'http://www.' + url in a_string: 
      # Links from the same site 
      found_link.append(a_string) 

     if 'https://www.' + url in a_string: 
      # Links from the same site with SSL 
      found_link.append(a_string) 
     #else: 
     # found_link.write(a_string + '\n') # testing only 
    output = found_link 

    return output 

# Function for removing duplicates 
def remove_duplicates(values): 
    output = [] 
    seen = set() 
    for value in values: 
     if value not in seen: 
      output.append(value) 
      seen.add(value) 
    return output 

# Run the function with our list in this order -> Makes the request -> Filters the links -> Removes duplicates 
def createURLList(values): 
    requests = makeRequest(values) 
    new_list = makeFilter(requests) 
    filtered_list = remove_duplicates(new_list) 

    return filtered_list 

result = createURLList(url) 

# print result 

# for verifying and crawling resulting pages 
for b in result: 
    sub_directories = createURLList(url + b) 
    crawler = [] 
    crawler.append(sub_directories) 

    print crawler 
+0

は、あなたは、 '' a_string' print'edことがありますか?それは空の文字列です。 – roganjosh

+0

私はそれを試してみましたが、3つの文字列を出力しても、同じエラーでエラーが出ますが、同じことが起こっています。 – George

+0

何かが見つからない限り、あなたのコードに 'print crawler'しかありません。 'print 'を試してください。これは' a_string = str(a) 'の真下のstring_a"、a'です。それはほぼ確実に空白です。 – roganjosh

答えて

1

if not a_string: 
    continue 
+0

これは実際に問題を解決したように見えますが、それでも約18のリンクがダウンしますが、エラーは異なります。ありがとう!それは空の文字列、どこかであることを意味するでしょうか? – George

+0

ええ、それは 'a_string'が偽(' '' '' '' '' 'False''や' 0'など)であることを意味します。 – Jack

+0

私は文字列関数を持たないとこれが良いのでしょうか、これは愚かな質問です。または私は掘り続けるべきですか? – George

関連する問題