免责声明:我完全不懂计算机科学,也不知道幕后发生的任何事情的内部工作原理。教我自己使用互联网上的所有东西来编写代码。在
Python版本:Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit(Intel)] on win32
在一个普通的解析器上工作,它的主要功能是获取图像的完整大小的url,将其保存到一个文件中供以后下载,然后移动到下一个图像中,这是非常强制性的,因为有问题的站点的web架构很差。当我完成程序时,在第976次执行时遇到了一个错误。在
^{pr2}$
经过研究,我发现问题是由“堆栈溢出”引起的。但是,目前我不知道如何在不造成任何显著性能下降的情况下工作。(不过,这其实不是什么问题,因为我只是为了学习而做的。)
这就引出了我的问题,我如何解决这个问题,在哪里可以了解更多关于这类事情的知识,比如从什么开始堆栈溢出?在
(程序运行良好,但堆栈溢出会停止它)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")