Python脚本未进入循环
我在网上找到的一个教程中写了一个脚本,基本上就是阅读新闻。脚本现在可以编译并启动,但实际上什么都不做。调试之后,我发现它没有进入for循环,我不知道为什么Python脚本未进入循环,python,beautifulsoup,raspberry-pi,Python,Beautifulsoup,Raspberry Pi,我在网上找到的一个教程中写了一个脚本,基本上就是阅读新闻。脚本现在可以编译并启动,但实际上什么都不做。调试之后,我发现它没有进入for循环,我不知道为什么 import subprocess import requests from bs4 import BeautifulSoup import textwrap head = 'mpg123 -q ' tail = ' &' url = 'http://www.ndtv.com/article/list/top-stories/'
import subprocess
import requests
from bs4 import BeautifulSoup
import textwrap
head = 'mpg123 -q '
tail = ' &'
url = 'http://www.ndtv.com/article/list/top-stories/'
r = requests.get(url)
soup = BeautifulSoup(r.content)
print '1'
g_data = soup.find_all("div",{"class":"natory_intro"})
log = open("/home/pi/logs/newslog.txt","w")
for item in g_data:
print '2'
#print >> log, item.text
shorts = textwrap.wrap(item.text, 100)
print '3'
for sentance in shorts:
print '4'
sendthis = sentance.join(['"http://translate.google.com/translate_tts?t1=en&q=', '"'])
print[head + sendthis + tail]
#print subprocess.call(head + sendthis + tail, shell=True)
print subprocess.check_output (head + sendthis + tail, shell=True)
print '5'
g_data
加载了来自网站的数据,因此g_data
中有一个项目,但它仍然没有被捕获到循环中。这可能是一个非常简单的修复,但我现在无法看到它。感谢您的帮助 你这行有一个打字错误
g_data = soup.find_all("div",{"class":"natory_intro"})
应该是
g_data = soup.find_all("div",{"class":"nstory_intro"})
我认为它没有进入循环,因为
g_data
是一个空列表
查看网站,确保您可以按类名获取项目“natory_intro”
我运行:
import subprocess
import requests
from bs4 import BeautifulSoup
import textwrap
head = 'mpg123 -q '
tail = ' &'
url = 'http://www.ndtv.com/article/list/top-stories/'
r = requests.get(url)
soup = BeautifulSoup(r.content)
g_data = soup.find_all("div",{"class":"natory_intro"})
print g_data
我明白了
编辑:
我想这就是你想要的:
#ins_storylist > ul > li:nth-child(1) > div.new_storylising_contentwrap > div.nstory_intro
只需将
“natory_intro”
更改为“nstory_intro”
,那么您看到了哪些打印
语句,哪些未到达?可能g_数据不可编辑?!你能检查一下你的g_数据类型吗?你从哪里得到natory_intro
?好的,很抱歉。我应该删除答案吗?啊,只是一个小的打字错误,这当然是我的问题。谢谢你,伙计
#ins_storylist > ul > li:nth-child(1) > div.new_storylising_contentwrap > div.nstory_intro