python3堆栈_避免python3中的堆栈溢出

免责声明:我完全不懂计算机科学,也不知道幕后发生的任何事情的内部工作原理。教我自己使用互联网上的所有东西来编写代码。在

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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值