gpt4 book ai didi

python - 从 Reddit 获取头条新闻

转载 作者:行者123 更新时间:2023-11-30 23:15:50 25 4
gpt4 key购买 nike

获得 Reddit 头版头条新闻的最佳方式是什么?目前,我正在使用 BeautifulSoup4 尝试抓取它们,但使用 Reddit API 似乎是一个可行的选择,但我在他们的文档中找不到要点击哪个 URL 来请求头条新闻。我的猜测是 http://www.reddit.com/r/frontpage/top.json?limit=10 之类的东西,但这不会在 frontpage 上产生任何标题。 .

Python Scraper 方法:(不起作用)

def scrape(url):                                                     
try:
req = urllib2.Request(url)
conn = urllib2.urlopen(req)
content = conn.read()

soup = BeautifulSoup(content)

for link in soup.find_all('a'):
print link
except urllib2.URLError, e:
print 'Your HTTP error response code is: ', e

有什么建议吗?

最佳答案

根据 @jonrsharpe 的评论,有一个 python Reddit API 客户端:

使用 get_top() 获取头条新闻:

>>> import praw
>>> r = praw.Reddit(user_agent='my_cool_application')
>>> for item in r.get_top():
... print item
...
4901 :: I made a Redundant Clock.
4764 :: Elon Musk plans to launch 4,000 satellites to deliver high-speed Inte...
5144 :: Pipeline breach spills up 50,000 gallons of oil into the Yellowstone ...
4603 :: Avalanche Dog In Training
4564 :: TIL it is illegal in many countries to perform surgical procedures on...
...

还有 get_top_from_day()get_top_from_hour()

关于python - 从 Reddit 获取头条新闻,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28033248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com