【发布时间】:2015-11-14 10:37:05
【问题描述】:
免责声明:我对计算机科学一无所知,也不了解幕后发生的任何事情的内部运作。使用互联网上的所有内容自学编码。
Python 版本:
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit(Intel)] on win32
使用普通解析器,其主要目的是获取图像的完整大小的 url,将其保存到文件中以供稍后下载,然后移动到行中的下一个图像,这几乎是强制性的,因为相关网站的不良网络架构。当我完成程序时,在第 976 次执行时遇到了一个错误。
RuntimeError: maximum recursion depth exceeded in comparison
经过研究,我发现问题是由于“堆栈溢出”引起的。但是,目前我不知道如何在不造成任何显着性能下降的情况下解决问题。 (不过,这不是问题,因为我只是为了学习。)
这让我想到了我的问题,我该如何解决这个问题,我在哪里可以了解更多关于这些事情的信息,比如 Stack Overflow 是从什么开始的?
(程序运行良好,但堆栈溢出停止)
import requests
from bs4 import BeautifulSoup
def somesite_parsing(url):
connection = requests.get(url)
html = connection.text
soup = BeautifulSoup(html, "html.parser")
# The exception is necessary due to the web architecture.
# Images that don't have different versions by size have an img tag.
# Returns "http://www.somesite.net/tag_tag_tag.full.jpg"
try:
semi_link = soup.select("html > body > #wrapper > #body > #content > #large > a")
full_link = semi_link[0].get("href")
print(full_link)
except IndexError:
semi_link = soup.select("html > body > #wrapper > #body > #content > #large > img")
full_link = semi_link[0].get("src")
print(full_link)
# File was created during testing so I switched to appending.
# Saves link into folder.
fx = open("list_file.txt", "a")
fx.write(full_link + "\n")
fx.close()
# Fetches the next url.
# Returns "/id_number"
next_link = soup.select("html > body > #wrapper > #body > #menu > .smallthumbs > li > a")
next_link = next_link[0].get("href")
next_link = "http://www.somesite.net" + next_link
print(next_link)
print()
somesite_parsing(next_link)
somesite_parsing("http://www.somesite.net/1905220")
【问题讨论】:
-
我假设最后一个电话,
zerochan_parsing,实际上应该是somesite_parsing? -
糟糕,没注意到 XD
-
每次再次调用该函数时,您总是调用
somesite_parsing。您需要确定一种停止呼叫somesite_parsing的方法。因此,也许可以尝试检查您是否仍然获得 id_number。如果您没有获得 id_number,则在再次调用somesite_parsing之前从函数中获得return
标签: python python-3.x stack beautifulsoup overflow