当我运行此代码时,我收到以下错误消息。
"错误:在parseres中,skipped_tokens = self._parse(timestr,** kwargs)
TypeError:' NoneType'对象不可迭代"
这是我的代码
from datetime import datetime
from dateutil import parser
import datetime
import feedparser
content = {}
content2= {}
content3= {}
source = ("http://news.yahoo.com/rss/", "http://rss.cnn.com/rss/edition.rss" )
for i in source:
content = feedparser.parse(i)
for feed in content.entries:
content2[feed.title] = [feed.published]
content3 = [(item[0], parser.parse(item[1])) for item in content2]
print content3
错误:
Traceback (most recent call last):
File "/Users/amirnakhostin/Documents/Computer Science /Python/HomeStudy.py", line 13, in <module>
content3 = [(item[0], parser.parse(item[1])) for item in content2]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.2-py2.7.egg/dateutil/parser.py", line 748, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.2-py2.7.egg/dateutil/parser.py", line 310, in parse
res, skipped_tokens = self._parse(timestr, **kwargs)
TypeError: 'NoneType' object is not iterable
答案 0 :(得分:0)
看看运行稍微修改过的程序时会发生什么:
from datetime import datetime
from dateutil import parser
import datetime
import feedparser
content = {}
content2= {}
content3= {}
source = ("http://news.yahoo.com/rss/", "http://rss.cnn.com/rss/edition.rss" )
for i in source:
content = feedparser.parse(i)
for feed in content.entries:
content2[feed.title] = [feed.published]
print content2
content3 = [(item[0], parser.parse(item[1])) for item in content2]
print content3
你得到:
[(u'B', u'o'), (u'B', u'l'), (u'W', u'h'), (u'W', u'o'), (u'U', u'S'), (u'M', u'o'), (u'T', u'i'), (u'H', u'o'), (u'7', u'0'), (u'G', u'o'), (u'H', u'i'), (u'I', u's'), (u'M', u'e'), (u'T', u'h'), (u'B', u'e'), (u'W', u'a'), (u'O', u'f'), (u'V', u'i'), (u'C', u'h'), (u'S', u't'), (u'F', u'a'), (u'L', u'e'), (u'S', u'u'), (u'B', u'e'), (u'P', u's'), (u'A', u'P'), (u'S', u't'), (u'M', u'i'), (u'I', u'c'), (u'F', u'a'), (u"'", u'D'), (u'E', u'P'), (u'M', u'a'), (u"'", u'G'), (u'C', u'l'), (u'L', u'a'), (u'8', u' '), (u'S', u'e'), (u'C', u'h'), (u'9', u' '), (u'A', u'n'), (u'A', u' '), (u'C', u'a'), (u'O', u'f'), (u'6', u' '), (u'C', u'h'), (u'1', u'9'), (u'W', u'a'), (u'T', u'h'), (u'G', u'o'), (u"'", u'T'), (u'L', u'o'), (u'M', u'a'), (u'D', u'o'), (u'U', u'.'), (u'P', u'o'), (u'I', u'd'), (u"'", u'B'), (u'C', u'h'), (u'U', u'S'), (u'D', u'i'), (u'E', u'x'), (u'I', u' '), (u'F', u'o'), (u'T', u'h'), (u'D', u'i'), (u'E', u'n'), (u'T', u'e'), (u'B', u'r'), (u'H', u'o'), (u'M', u'o'), (u"'", u'H'), (u'W', u'o'), (u'M', u'u'), (u'5', u'3'), (u'M', u'a'), (u'W', u'e'), (u'W', u'h'), (u'A', u'r'), (u'E', u'a'), (u'B', u'e'), (u'F', u'i'), (u'I', u'n'), (u'G', u'r'), (u'U', u'.'), (u'S', u'e'), (u'T', u'h'), (u'M', u'i'), (u'W', u'h'), (u'C', u'h'), (u'S', u'p'), (u'F', u'r'), (u'T', u'i'), (u'A', u'c'), (u"'", u'B'), (u'2', u'0'), (u'H', u'a'), (u'G', u'l'), (u'W', u'h'), (u'M', u'u'), (u'C', u'N'), (u'B', u'e'), (u'A', u'r'), (u'M', u'o'), (u'K', u'i'), (u'T', u'u'), (u'D', u'o'), (u'A', u'm'), (u'A', u' '), (u'D', u'e'), (u'H', u'o'), (u'W', u'h'), (u'W', u'i'), (u'H', u'o'), (u'$', u'2'), (u'Y', u'o'), (u'5', u'3'), (u'S', u't'), (u'S', u'c'), (u'H', u'o'), (u'P', u'r'), (u'V', u'o'), (u'M', u'e'), (u'V', u'.'), (u'M', u'a'), (u"'", u'G'), (u'L', u'o'), (u'F', u'r'), (u'H', u'o'), (u'M', u'e'), (u'E', u'c'), (u"'", u'B'), (u'S', u'.'), (u'T', u'h'), (u'G', u'l'), (u'A', u'P'), (u'A', u'm'), (u'R', u'u'), (u'M', u'a'), (u'T', u'e'), (u'G', u'i'), (u'I', u's'), (u'P', u'h'), (u'O', u'f'), (u'M', u'i'), (u'B', u'o'), (u'C', u'h'), (u'R', u'e'), (u'W', u'o'), (u'R', u'i')]
查看content2
中的第一个条目:
{u'Bowe Bergdahl': [u'Sun, 01 Jun 2014 17:02:05 EDT']
相反,你可能想要:
from datetime import datetime
from dateutil import parser
import datetime
import feedparser
content = {}
content2= {}
content3= {}
source = ("http://news.yahoo.com/rss/", "http://rss.cnn.com/rss/edition.rss" )
for i in source:
content = feedparser.parse(i)
for feed in content.entries:
content2[feed.title] = [feed.published]
print content2
content3 = [(key, parser.parse(val[0])) for (key,val) in content2.iteritems()]
print content3
这给出了:
[(u'Bowe Bergdahl', datetime.datetime(2014, 6, 1, 17, 2, 5)), (u'Blast kills 18 in Nigeria', datetime.datetime(2014, 6, 1, 20, 16, 8)), (u'Who did U.S. swap Bergdahl for?', datetime.datetime(2014, 6, 1, 22, 51, 43)), (u'World Cup forces out drug gangs', datetime.datetime(2014, 5, 31, 22, 5, 38)), (u'US defends captive swap with Taliban, critics stir', datetime.datetime(2014, 6, 1, 15, 56, 50, tzinfo=tzoffset(None, -14400))), (u'Model fights Google over porn photos', datetime.datetime(2014, 5, 31, 23, 27, 49)), (u'Hong Kong rallies for democracy', datetime.datetime(2014, 6, 1, 23, 28, 51)), (u"70 years after D-Day, she hears dad's stories anew", datetime.datetime(2014, 5, 31, 15, 36, 9, tzinfo=tzoffset(None, -14400))), (u"Go inside Brazil's war on drugs", datetime.datetime(2014, 5, 30, 11, 23, 10)), (u'Hiroshima bomb site draws crowds', datetime.datetime(2014, 6, 1, 22, 44, 37)), (u'Is Mideast the new global aviation hub?', datetime.datetime(2014, 5, 31, 14, 21, 18)), (u'Meteors streak through night sky', datetime.datetime(2014, 5, 27, 7, 11, 11)), (u'Thumbs up', datetime.datetime(2014, 5, 30, 8, 27, 41)), (u"Bergdahl's parents: 'It isn't over'", datetime.datetime(2014, 6, 2, 1, 53, 21)), (u'Walter White and the undead', datetime.datetime(2014, 6, 1, 7, 49, 15)), (u'Officials: 7 aboard NJ-bound plane die in crash', datetime.datetime(2014, 6, 1, 3, 54, 1, tzinfo=tzoffset(None, -14400))), (u'Visionary concept cars', datetime.datetime(2014, 5, 28, 7, 13, 7)), (u'Chinese hackers infiltrated U.S.', datetime.datetime(2014, 5, 19, 23, 0, 51)), (u'Store defends flag likened to KKK hood', datetime.datetime(2014, 5, 30, 22, 0, 19)), (u"Family 'wanted $1000 not to kill wife'", datetime.datetime(2014, 5, 31, 11, 35, 1)), (u'Learning to talk with autism', datetime.datetime(2014, 4, 30, 9, 41, 40)), (u'Suspect in Jewish museum killings went to Syria', datetime.datetime(2014, 6, 1, 18, 46, 2, tzinfo=tzoffset(None, -14400))), (u"Bergdahl's mother: 'It's a good day'", datetime.datetime(2014, 6, 1, 9, 2, 23)), (u'Psychopaths: Better in business?', datetime.datetime(2014, 5, 31, 12, 52, 58)), (u'AP PHOTOS: Iran mineral baths offer relief', datetime.datetime(2014, 6, 2, 2, 21, 41, tzinfo=tzoffset(None, -14400))), (u'Sterling sues NBA for $1B', datetime.datetime(2014, 5, 31, 23, 33, 50)), (u"Miley Cyrus' home robbed, police say", datetime.datetime(2014, 6, 1, 19, 34, 34)), (u"Iceland's awesome Westfjords", datetime.datetime(2014, 5, 31, 14, 23, 1)), (u"Family's plea: Hang rapists", datetime.datetime(2014, 6, 2, 2, 8)), (u"'Drug dealers' of the sticker world", datetime.datetime(2014, 6, 1, 7, 45, 58)), (u'EPA seeks to cut power plant carbon by 30 percent', datetime.datetime(2014, 6, 2, 0, 4, 37, tzinfo=tzoffset(None, -14400))), (u'Mastering cyber-espionage', datetime.datetime(2014, 5, 30, 11, 25, 14)), (u"'Gangnam' hits 2 billion YouTube views", datetime.datetime(2014, 6, 1, 14, 32, 39)), (u'Clues sought in deadly Massachusetts jet crash', datetime.datetime(2014, 6, 2, 2, 34, 22, tzinfo=tzoffset(None, -14400))), (u'Lack of empathy a bonus', datetime.datetime(2014, 5, 31, 12, 53, 47)), (u'8 charged over Tiananmen attack', datetime.datetime(2014, 6, 1, 1, 26, 51)), (u'Secret eyes watching you shop', datetime.datetime(2014, 5, 31, 7, 33, 31)), (u'China, Vietnam tensions rise', datetime.datetime(2014, 5, 30, 1, 26, 31)), (u'9 most controversial foods', datetime.datetime(2014, 6, 1, 1, 39, 28)), (u"Andy Warhol's lost art", datetime.datetime(2014, 4, 25, 11, 11, 30)), (u'A one of a kind friendship', datetime.datetime(2014, 2, 21, 8, 18, 8)), (u'Campaigner quits after expose', datetime.datetime(2014, 5, 31, 7, 9, 28)), (u'Official: Recovering fallen climbers too dangerous', datetime.datetime(2014, 6, 1, 20, 25, 34, tzinfo=tzoffset(None, -14400))), (u'6 climbers missing on U.S. mountain', datetime.datetime(2014, 6, 1, 14, 31, 52)), (u"Chuck Hagel: Captive's life was in danger", datetime.datetime(2014, 6, 1, 5, 26, 4, tzinfo=tzoffset(None, -14400))), (u'Was caste to blame?', datetime.datetime(2014, 6, 2, 2, 29, 23)), (u"Thai protesters come out again despite junta's ban", datetime.datetime(2014, 6, 1, 8, 17, 23, tzinfo=tzoffset(None, -14400))), (u"Google's car is the 'future'", datetime.datetime(2014, 5, 30, 11, 22, 39)), (u"'Too sexy' yearbook photos edited?", datetime.datetime(2014, 5, 31, 13, 26, 44)), (u'Long road to recovery for POWs', datetime.datetime(2014, 6, 1, 15, 5, 21)), (u'Man arrested over museum attack', datetime.datetime(2014, 6, 1, 21, 6, 15)), (u'Tackling sanitation', datetime.datetime(2014, 6, 2, 2, 33, 43)), (u'Doh! 20 biggest travel mistakes', datetime.datetime(2014, 5, 31, 5, 22, 36)), (u"U.S. 'hypocrisy' in cybertheft charge", datetime.datetime(2014, 5, 23, 2, 30, 44)), (u"Poll: More Americans oppose Snowden's actions", datetime.datetime(2014, 6, 1, 18, 41, 50, tzinfo=tzoffset(None, -14400))), (u'Idaho town prepares for homecoming', datetime.datetime(2014, 6, 1, 9, 2, 23)), (u"'Brady Bunch' star Ann B. Davis dies", datetime.datetime(2014, 6, 1, 21, 43, 58)), (u'China, Vietnam face off in S. China Sea', datetime.datetime(2014, 5, 31, 23, 54, 48)), (u'US soldier freed from captivity in Afghanistan', datetime.datetime(2014, 5, 31, 22, 40, 7, tzinfo=tzoffset(None, -14400))), (u'Did sex traffic hero invent story?', datetime.datetime(2014, 5, 31, 12, 52, 14)), (u'Extremists busted in Xinjiang', datetime.datetime(2014, 5, 30, 1, 23, 59)), (u"I was an accomplice to my brother's suicide", datetime.datetime(2014, 5, 6, 11, 21, 48)), (u'Former NYC Mayor Bloomberg calls for stricter gun laws', datetime.datetime(2014, 6, 1, 11, 53, 29, tzinfo=tzoffset(None, -14400))), (u"The world's most incredible origami", datetime.datetime(2014, 6, 1, 15, 26, 35)), (u'Did Qatar buy 2012 World Cup?', datetime.datetime(2014, 6, 2, 2, 5, 23)), (u"Enter into Congo's cult of elegance", datetime.datetime(2014, 2, 22, 5, 43, 59)), (u'Teen attempts record solo RTW flight', datetime.datetime(2014, 6, 1, 14, 36, 44)), (u'Brothers confess to gang rape in India', datetime.datetime(2014, 6, 2, 1, 55, 3)), (u'How to train like a top cyclist', datetime.datetime(2014, 6, 1, 7, 46, 52)), (u'Mothers reveal how to raise a winner', datetime.datetime(2014, 6, 1, 11, 42, 42)), (u"'Horse quidditch' seeks Olympic spot", datetime.datetime(2014, 6, 1, 11, 20, 46)), (u"World Cup's most exotic city awaits tourist influx", datetime.datetime(2014, 6, 2, 0, 8, 38, tzinfo=tzoffset(None, -14400))), (u'Museum shooting', datetime.datetime(2014, 6, 1, 14, 9, 23)), (u'53 spectacular wildlife photos', datetime.datetime(2014, 5, 30, 6, 18, 5)), (u'Maya Angelou film set for release', datetime.datetime(2014, 6, 1, 11, 58, 32)), (u'We need birds, and birds need us', datetime.datetime(2014, 5, 31, 7, 33, 42)), (u'What does sentence say about Sudan?', datetime.datetime(2014, 6, 1, 7, 37, 24)), (u'Are old cellphones a new fad?', datetime.datetime(2014, 6, 1, 7, 43, 28)), (u'Easy for Rafa, Murray on brink', datetime.datetime(2014, 5, 31, 22, 0, 52)), (u'Best wildlife trips in the U.S.', datetime.datetime(2014, 5, 31, 11, 36, 7)), (u'Fisherman reels in rare saw shark', datetime.datetime(2014, 6, 1, 15, 22, 54)), (u'India gang rape', datetime.datetime(2014, 6, 1, 20, 53, 33)), (u"Grief and fear in girls' village", datetime.datetime(2014, 6, 1, 21, 12, 22)), (u'U.S. soldier freed in Afghanistan, 5 Taliban prisoners leave Gitmo', datetime.datetime(2014, 6, 1, 15, 59, 6, tzinfo=tzoffset(None, -14400))), (u'Sex trafficking hero quits after expose', datetime.datetime(2014, 5, 31, 10, 41, 3)), (u'The shocking truth about tobacco', datetime.datetime(2014, 5, 31, 23, 53, 48)), (u'Mickelson denies fraud allegations', datetime.datetime(2014, 5, 31, 21, 53, 35)), (u"What are 'firenadoes'?", datetime.datetime(2014, 5, 16, 13, 38, 44)), (u"China's blind head to exam hall", datetime.datetime(2014, 5, 19, 23, 1, 14)), (u'Spelling Bee crowns two W-I-N-N-E-R-S', datetime.datetime(2014, 6, 1, 11, 28, 47)), (u'From humble GIF to high art', datetime.datetime(2014, 4, 17, 12, 37, 32)), (u'Tiananmen Square attack: 8 charged', datetime.datetime(2014, 5, 31, 12, 19, 11)), (u'Accused Brad Pitt attacker fired', datetime.datetime(2014, 6, 1, 0, 16, 27)), (u"'Brady Bunch' actress Ann B. Davis dies in Texas", datetime.datetime(2014, 6, 2, 1, 3, 20, tzinfo=tzoffset(None, -14400))), (u'20 biggest travel mistakes', datetime.datetime(2014, 6, 1, 1, 37, 20)), (u'Hackers use social media to spy', datetime.datetime(2014, 5, 31, 3, 4, 33)), (u'Glamorous Orient Express', datetime.datetime(2014, 4, 30, 11, 50, 43)), (u"Where is S. Korea's most wanted man?", datetime.datetime(2014, 6, 1, 7, 33, 47)), (u'Museum gunman caught on video', datetime.datetime(2014, 6, 1, 15, 14, 25)), (u'CNN reporter held by police on air', datetime.datetime(2014, 6, 1, 11, 18, 33)), (u'Bergdahl taken to German hospital', datetime.datetime(2014, 6, 1, 15, 6, 57)), (u'Are you a psychopath? Take the test', datetime.datetime(2014, 5, 31, 12, 50, 1)), (u'Most controversial foods', datetime.datetime(2014, 5, 31, 14, 23, 39)), (u'Kings beat Blackhawks 5-4 in OT in Game 7', datetime.datetime(2014, 6, 2, 0, 51, 40, tzinfo=tzoffset(None, -14400))), (u'Turkish cops harass CNN reporter', datetime.datetime(2014, 5, 31, 23, 30, 43)), (u'Do wearable fitness gadgets work?', datetime.datetime(2014, 6, 1, 15, 24, 52)), (u'Americans fighting for al Qaeda', datetime.datetime(2014, 6, 2, 0, 9, 5)), (u'A billion dollar artwork?', datetime.datetime(2014, 5, 14, 7, 4, 17)), (u'Death sentence for tycoon', datetime.datetime(2014, 5, 23, 2, 30, 27)), (u'How to buy a great watch', datetime.datetime(2014, 3, 31, 13, 40, 44)), (u'Why superheroes are good for kids', datetime.datetime(2014, 4, 29, 11, 15, 43)), (u'Will monks ban interfaith marriage?', datetime.datetime(2014, 6, 1, 2, 33, 1)), (u"How to win China's tourists", datetime.datetime(2014, 5, 30, 1, 30, 48)), (u"$2B for the Clippers? You're kidding", datetime.datetime(2014, 5, 31, 20, 36, 5)), (u"You're never too young to be CEO", datetime.datetime(2014, 5, 19, 11, 7, 18)), (u'53 spectacular wildlife shots', datetime.datetime(2014, 5, 30, 6, 14, 3)), (u'Sterling sues NBA for $1 billion', datetime.datetime(2014, 5, 31, 2, 10)), (u'Scenes from the field', datetime.datetime(2013, 12, 6, 3, 12, 27)), (u'Private plane crash kills 7 in U.S.', datetime.datetime(2014, 6, 1, 20, 17, 28)), (u'Volcano grounds flights in Asia', datetime.datetime(2014, 6, 2, 2, 35, 52)), (u"Meet India's next PM", datetime.datetime(2014, 5, 16, 13, 38, 32)), (u'V. Stiviano assaulted in New York', datetime.datetime(2014, 6, 2, 1, 24, 46)), (u'Man catches baby after fall', datetime.datetime(2014, 5, 30, 1, 28, 57)), (u"'Gangnam' milestone", datetime.datetime(2014, 6, 1, 14, 32, 39)), (u"Look, no driver: Google's new car", datetime.datetime(2014, 5, 31, 13, 29, 27)), (u'Without toilets, women are vulnerable', datetime.datetime(2014, 6, 2, 2, 29, 42)), (u"France: Suspect in Brussels' Jewish Museum shooting arrested", datetime.datetime(2014, 6, 1, 7, 27, 59, tzinfo=tzoffset(None, -14400))), (u'How to make cash from trash', datetime.datetime(2014, 5, 31, 6, 18, 53)), (u'Meet the armless table tennis champ', datetime.datetime(2014, 6, 1, 15, 39, 40)), (u"Eco wonders from 'shabby' mines", datetime.datetime(2014, 5, 19, 23, 1, 56)), (u"'Brady Brunch' actress Ann B. Davis dies in Texas", datetime.datetime(2014, 6, 1, 20, 4, 59, tzinfo=tzoffset(None, -14400))), (u'S. Korean sentenced in North Korea', datetime.datetime(2014, 6, 2, 2, 33, 5)), (u"The world's top furniture fair", datetime.datetime(2014, 4, 14, 7, 2, 53)), (u'Glass floor cracks under tourists', datetime.datetime(2014, 5, 30, 11, 17, 7)), (u'AP PHOTOS: Teams, fans counting down to World Cup', datetime.datetime(2014, 6, 2, 0, 2, 31, tzinfo=tzoffset(None, -14400))), (u'American suicide bomber in Syria?', datetime.datetime(2014, 5, 30, 11, 17, 58)), (u'Russian proton rocket fails', datetime.datetime(2014, 5, 16, 13, 33, 53)), (u'Mastery of Easter eggs', datetime.datetime(2014, 4, 24, 7, 32, 38)), (u'Terror arrest at Heathrow Airport', datetime.datetime(2014, 5, 31, 22, 0, 20)), (u'Gitmo detainees', datetime.datetime(2014, 6, 1, 11, 51, 7)), (u"Is this the return of 'White Plague'?", datetime.datetime(2014, 5, 31, 2, 47, 9)), (u'Philadelphia Inquirer co-owner dead in plane crash', datetime.datetime(2014, 6, 1, 9, 54, 17, tzinfo=tzoffset(None, -14400))), (u'Official: 6 climbers likely died in mountain fall', datetime.datetime(2014, 6, 1, 14, 27, 5, tzinfo=tzoffset(None, -14400))), (u"Mickelson: I've done nothing wrong", datetime.datetime(2014, 5, 31, 22, 6, 32)), (u'Bowe Bergdahl: Hero or deserter?', datetime.datetime(2014, 6, 2, 1, 48, 9)), (u'China faces war on terror', datetime.datetime(2014, 5, 25, 22, 35, 52)), (u'Report: NSA collects facial images', datetime.datetime(2014, 6, 1, 21, 9, 15)), (u"World's 10 most underrated cities", datetime.datetime(2014, 6, 1, 11, 19, 23)), (u'Rise of the mega-museums', datetime.datetime(2014, 4, 10, 15, 51, 38))]
或许你想让它成为一个词典?