Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-无输出或错误消息_Python - Fatal编程技术网

python-无输出或错误消息

python-无输出或错误消息,python,Python,当我尝试运行上述代码时,没有显示任何错误消息。唯一显示的是runfile(myfilepath),wdir(另一个路径)。我希望输出打印名称,在本例中为“Donald Trump”。有什么帮助或建议吗?我使用spyder(python 3.6)作为我的平台 这部分代码 import requests from bs4 import BeautifulSoup url = "https://twitter.com/realDonaldTrump?ref_src=twsrc%5Egoogle%7C

当我尝试运行上述代码时,没有显示任何错误消息。唯一显示的是runfile(myfilepath),wdir(另一个路径)。我希望输出打印名称,在本例中为“Donald Trump”。有什么帮助或建议吗?我使用spyder(python 3.6)作为我的平台

这部分代码

import requests
from bs4 import BeautifulSoup

url = "https://twitter.com/realDonaldTrump?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"

r = requests.get(url)

soup = BeautifulSoup(r.content, "html.parser")

links = soup.find_all("a")

g_data = soup.find_all("div", {"class": "content"})  

n_data = soup.find_all("strong", {"class": "fullname show-popup-with-id "})

c_data = soup.find_all("p", {"class": "TweetTextSize TweetTextSize--normal js-tweet-text tweet-text"})

for item in g_data:

    try:

        print(item.contents[0].find_all(n_data)[0].text)

    except:

        pass
有效地抑制错误消息

您的
print(item.contents[0].find_all(n_data)[0].text)
应该是
print(item.find_all(“strong”,“class”:“全名显示带id的弹出窗口”})[0].text)

输出将是

Donald J.Trump
...
...
...

唐纳德·J·特朗普

你实际上用try块消除了所有错误。你这是什么意思?顺便说一句,为了更好的视觉效果,我把上面的代码隔开了。嗨,我试着去掉异常,结果会是“解析时出现意外的EOF”。但当我进行尝试时,我会得到“'navigablesting'对象没有属性'find_all'”。很抱歉,如果这是一个愚蠢的问题,但我对python完全陌生。
try:
需要
除外:
,因此“解析时意外的EOF”(EOF表示文件结束)-解析器需要
除外:
,但它从未到达。非常感谢您的解释!我不知怎的期待它,但仍然尝试和错误,看看它是否工作。谢谢你!嗨,我试过了,它通过了!非常感谢你
    except:

        pass
import requests
from bs4 import BeautifulSoup

url = "https://twitter.com/realDonaldTrump?ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor"

r = requests.get(url)
soup = BeautifulSoup(r.content, "html.parser")

links = soup.find_all("a")
g_data = soup.find_all("div", {"class": "content"})  
n_data = soup.find_all("strong", {"class": "fullname show-popup-with-id "}) 
c_data = soup.find_all("p", {"class": "TweetTextSize TweetTextSize--normal js-tweet-text tweet-text"})

for item in g_data:
    try:
        print(item.find_all("strong", {"class": "fullname show-popup-with-id "})[0].text) # chain the n_data here
    except:
        pass