错误:AttributeError:'NoneType'对象没有属性'text'

时间:2016-05-28 21:43:47

标签: python multithreading python-3.x web-scraping

我必须使用simillar代码,一个有多线程,另一个没有。多线程的一个是收到此错误 AttributeError:'NoneType'对象没有属性'text'而另一个不是,这是我的代码:

多线程:

import threading
import requests
from bs4 import BeautifulSoup

symbolsfile = open("Stocklist.txt")

symbolslist = symbolsfile.read()

thesymbolslist = symbolslist.split("\n")

print (thesymbolslist)


print_lock = threading.Lock()

def th(ur):
    theurl = "http://money.cnn.com/quote/quote.html?symb=" + ur
    thepage = requests.get(theurl)
    soup = BeautifulSoup(thepage.content,"html.parser")
    textfind = soup.find('span',{"stream":"last_36276"})
    texttext = textfind.text
    with print_lock:
        print(textfind)

threadlist = []

for u in thesymbolslist:
    t = threading.Thread(target = th, args=(u,))
    t.start()

    threadlist.append(t)

for b in threadlist:
    b.join()

和没有多线程的那个:

import requests
from bs4 import BeautifulSoup


theurl = "http://money.cnn.com/quote/quote.html?symb=" + "AAPL"
thepage = requests.get(theurl)
soup = BeautifulSoup(thepage.content,"html.parser")
textfind = soup.find('span',{"stream":"last_36276"})
texttext = textfind.text
print(texttext)

1 个答案:

答案 0 :(得分:0)

symbolslist设置为['AAPL']时,多线程代码在我的系统(Win10,Python 3.4-64bit)上运行得非常好。但是,当symbolslist设置为['AAPL', 'IBM']时,脚本会因报告的错误而崩溃。

检查返回的HTML代码时,您可以看到使用stream="last_151846"stream="last_36276"一次。将textfind行更改为

textfind = soup.find('span',{"streamformat":"ToHundredth"})

textfind = soup.find('span',{"streamfeed":"SunGard"})

代码适用于这些示例,并希望用于Stocklist.txt中提供的代码。