我必须使用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)
答案 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
中提供的代码。