- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一个网络爬虫。我没有使用 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
最佳答案
在a_string = str(a)
之后尝试添加:
if not a_string:
continue
关于python - 网络爬虫问题 : IndexError: string index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454811/
我在一个网站上工作。 我需要了解我的网站是否获得了来自 Google 或任何其他搜索引擎的抓取工具/机器人的访问 在我的应用程序中,我正在拦截 http 请求。并且需要查明爬虫/机器人是否正在发出 h
我与北京理工大学python爬虫的邂逅 在人生的旅途中,我们时常有机会邂逅一些惊艳的事物,它们仿佛一束亮光照亮了前行的道路。而我,一个平凡的人类,也不例外。回想起那个让我心动的时刻,我想起了与北京理工
从“爬虫”谈起 在这个信息爆炸的时代,互联网上的数据宛如茫茫星海,琳琅满目。但是,我们能否想象到这些数据背后隐藏着多少珍贵的信息呢?有人说,要想获得这些数据,我们需要借助一种神奇的力量——爬虫。 什么
探索未知的领域 曾几何时,我陷入了对互联网世界的好奇之中。作为一个普通的人类,我对于计算机技术的了解可谓是一窍不通。然而,好奇心驱使着我踏上了学习的征途。 奇妙的爬虫 在探索计算机领域的过程中,我翻阅
教室的奇妙之旅 在一个晴朗的早晨,我来到了一所神奇且富有魅力的教室。这里充满了未知的冒险和无限的学习可能。嘿,你没听错,就是那个零基础Python视频教程课程的教室!让我带你一起踏上这趟令人期待的爬虫
零基础学python3 爬虫 嗨,各位小伙伴们!今天要和大家分享一段有关学习Python3爬虫的故事。无论是技术小白还是编程高手,都可以从中获益良多。 1. 邂逅神奇的爬虫世界 就像是迷失在未知之地的
我正在开发一个 Python(3) 程序,在该程序中,我必须使用输入作为表示各种操作的多次迭代列表来返回目录迭代的移动次数,例如: ../ 表示移动到当前文件夹的父文件夹。 ./ 保留在同一文件夹中
我和Python:一段关于爬虫和第三方库的故事 曾经有一个人,他和一门编程语言结下了不解之缘。这门语言就是Python,一个灵活而强大的工具,让他的世界变得多姿多彩。 遇见爬虫:探索未知的世界 某天,
偷心技艺:大众点评TOP10 爬虫 Python 故事发生在一个被互联网包围的城市,充满了各种各样的餐馆、咖啡厅和美食街巷。每天都有无数的食客们纠结于选择哪家餐厅才能获得满足和享受。就在这个时候,我偶
登山寻宝:为什么Python爬虫部分网页源码 人生就像登山寻宝的旅程,充满了未知和挑战。而对于爱好编程的我来说,写一个Python爬虫,探索网络世界也是一种刺激的冒险。今天,我想和大家分享一下为什么要
插班生的悲喜交加 记得那个夏天,我踏入了编程的殿堂,成为了一名程序员。对于一个毫无基础的人来说,这是一次勇敢而激动的尝试。然而,更令我兴奋的是,在我的码农之旅中,我发现了一种神奇的力量——Python
一、 意外的宝藏 当我踏进了编程的大门,发现了那个绚丽多彩的世界时,我仿佛捡到了一个意外的宝藏。在编码的世界里,我像是一个魔术师,用着神奇的符号与逻辑来创造奇迹。然而,随着时间的推移,我渐渐找到了一种
“呜呜呜~” 夏日的阳光穿过微风,洒在我身上,仿佛一片温暖而宁静的海洋。我望着眼前充满希望与挑战的屏幕,满脸的期待和困惑。作为一个刚刚入门的编程初学者,我竭尽全力探索着那座神秘的编程世界,好像是航行在
我的编程之旅 嗨,大家好!我是一个普通的人类,对于编程世界充满着无限的好奇和热情。今天,我想向大家讲述一段关于我的编程经历,特别是在爬虫领域的探索之旅。 1. 踏上未知的征途 就像一只踏上大海的小船,
揭秘神奇的爬虫世界:探寻京东销量数据库 从古至今,人类一直渴望了解世界的各个角落。而对于互联网这个充满未知和神秘的领域来说,我们更是怀着好奇和充满冒险精神的心态去探索。今天,我将带领大家踏上一段激动人
啊!你好呀!很高兴能遇到你,让我向你讲一个关于python爬虫爬取关键词排名的故事。 章节一:奇妙的网络世界 网络就像一片浩瀚的海洋,里面有无数宝藏等待着我们发掘。而当我们学会了使用python这把钥
一次意外的奇遇 在我们生活的世界中,技术如今已经无处不在,无论是大型软件开发还是个人应用,都离不开它的支持。而其中最为神奇的领域之一,就是爬虫技术。想象一下,你有可能仅凭几行代码,就能够在电脑屏幕上将
奇遇之旅 在这个大千世界中,我有幸踏上了一次令人兴奋的冒险之旅。故事的开始,是一个寂静而神秘的夜晚。 迷宫的入口 深夜的城市,街道上弥漫着微弱的灯光,仿佛点缀着无数的星星。我沿着曲径通幽的小巷走着,脚
python 爬虫 自动上传下载 从前有一个热爱编程的小伙子,他喜欢利用自己的技术解决各种实际问题。有一天,他面对一个任务:需要在网络上批量上传和下载文件。这可不是一件轻松的工作,但对于这位小伙子来说
一场奇妙的探险之旅:Python编程语言中的爬虫第三方库 故事从一个寻宝者开始。他身穿一件斑驳的皮衣,手持一柄锈迹斑斑的剑,如同一个当代版的荒野游侠。这位勇士的目标是探索Python编程语言中的神秘领
我是一名优秀的程序员,十分优秀!