【问题标题】:Issue with web crawler: IndexError: string index out of range网络爬虫问题:IndexError:字符串索引超出范围
【发布时间】:2017-01-04 01:19:36
【问题描述】:

我正在制作一个网络爬虫。我没有使用scrapy或任何东西,我试图让我的脚本做大部分事情。我已尝试搜索该问题,但似乎找不到任何有助于解决该错误的内容。我尝试切换一些变量以尝试缩小问题范围。我在第 24 行收到错误提示 IndexError: string index out of range。这些函数在第一个 url(原始 url)上运行,然后是第二个,在原始数组中的第三个上失败。我迷路了,任何帮助将不胜感激!请注意,我只是将它们全部打印出来进行测试,我最终会将它们打印到文本文件中。

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

【问题讨论】:

  • 你有printed a_string吗?这是一个空字符串。
  • 我确实尝试过,但我得到了同样的结果,尽管它打印了三个字符串,然后出现同样的错误。
  • 除非我遗漏了什么,否则你的代码中只有print crawler。在a_string = str(a) 下方尝试print "this is string_a", a。它几乎肯定是空白的。
  • 好的,我做到了,虽然它不是空的,但我从它找到的所有链接中打印出来。
  • 老实说,我不知道有任何其他方法可以得到IndexError: string index out of range,除非你有this is string a ,并且在错误发生前立即打印出来之后什么也没有。您可以尝试 import sys 并在 print 语句之后和 if a_string[0] == '/' 之前执行 sys.stdout.flush() 以检查您是否正确看到最后一个 print 语句。

标签: python index-error


【解决方案1】:

a_string = str(a)之后尝试添加:

if not a_string:
  continue

【讨论】:

  • 这实际上看起来可能已经解决了问题,它仍然失败了大约 18 个链接,但错误是不同的。谢谢!这是否意味着它在某个地方是一个空字符串?
  • 是的,这意味着a_string 是假的(""NoneFalse0 等),但几乎可以肯定是一个空字符串。
  • 很抱歉这是一个愚蠢的问题,如果我没有字符串功能会更好吗?还是我应该继续挖掘?
  • @GeorgeOffley 应该没关系。现在哪里/什么错误?
  • 这是新错误。正如我所说,脚本运行了大约十八个链接。 requests.exceptions.ConnectionError: HTTPConnectionPool(host='python.orghttp', port=80): Max retries exceeded with url: //python.org/dev/peps/ (由 NewConnectionError(':无法建立新连接:[Errno -2] Name or service not known',))
猜你喜欢
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多