【发布时间】:2014-04-18 16:11:13
【问题描述】:
我有一个 RSS/ATOM 提要 URL 列表(近 500 个)来解析和获取链接。
我正在使用 python feedparser 库来解析 url。为了并行解析url列表,我想到了在python中使用线程库。
我的代码看起来像这样
import threading
import feedparser
class PullFeeds:
def _init__(self):
self.data = open('urls.txt', 'r')
def pullfeed(self):
threads = []
for url in self.data:
t = RssParser(url)
threads.append(t)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
class RssParser(threading.Thread):
def __init__(self, url):
threading.Thread.__init__(self)
self.url = url
def run(self):
print "Starting: ", self.name
rss_data = feedparser.parse(self.url)
for entry in rss_data.get('entries'):
print entry.get('link')
print "Exiting: ", self.name
pf = PullFeeds()
pf.pullfeed()
问题是,当我运行此脚本时,Feedparser 会返回一个空列表。但是在没有线程的情况下,feedparser 会打印出从提供的 URL 解析的链接列表。
我该如何解决这个问题?
【问题讨论】:
标签: python multithreading feedparser