python3中的分段错误

时间:2016-06-24 00:27:22

标签: python-3.x segmentation-fault

我在Ubuntu机器上运行python3,并注意到以下代码块是变幻无常的。有时它运行得很好,有时它会产生分段错误。我不明白为什么。有人可以解释可能会发生什么吗?

基本上,代码所做的是尝试从维基百科阅读S& P公司,并将代码列表写入与脚本相同的目录中的文件。如果无法建立与Wikipedia的连接,则脚本会尝试从文件中读取现有列表。

from urllib import request
from urllib.error import URLError
from bs4 import BeautifulSoup
import os
import pickle
import dateutil.relativedelta as dr
import sys

sys.setrecursionlimit(100000)

def get_standard_and_poors_500_constituents():
    fname = (
        os.path.abspath(os.path.dirname(__file__)) + "/sp500_constituents.pkl"
    )
    try:
        # URL request, URL opener, read content.
        req = request.Request(
            "http://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
        )
        opener = request.urlopen(req)
        # Convert bytes to UTF-8.
        content = opener.read().decode()
        soup = BeautifulSoup(content, "lxml")
        # HTML table we actually need is the first.
        tables = soup.find_all("table")     
        external_class = tables[0].findAll("a", {"class":"external text"})
        c = [ext.string for ext in external_class if not "reports" in ext]

        with open(fname, "wb") as f:
            pickle.dump(c, f)

    except URLError:
        with open(fname, "rb") as f:
            c = pickle.load(f)
    finally:
        return c


sp500_constituents = get_standard_and_poors_500_constituents()
spdr_etf = "SPY"
sp500_index = "^GSPC"

def main():
    X = get_standard_and_poors_500_constituents()
    print(X)

if __name__ == "__main__":
    main()

0 个答案:

没有答案