繁体   English   中英

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

[英]Error: AttributeError: 'NoneType' object has no attribute 'text'

我必须使用类似的代码,一个有多线程,另一个没有。 多线程的一个得到这个错误AttributeError: 'NoneType' object has no attribute '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)

symbolslist设置为['AAPL']时,多行代码在我的系统(Win10,Python 3.4-64位)上可以symbolslist 但是,当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提供的Stocklist.txt

如果您的问题包括版本 11.0.1 中的 pytube。 我建议你看看这个帖子:

pytube: AttributeError: 'NoneType' 对象没有属性 'span'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM