subreddits的美化组

2024-06-02 10:55:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用beauthoulsoup学习一些html解析,并试图让它为reddit工作。这是我的密码

!/usr/bin/python

import BeautifulSoup
from BeautifulSoup import BeautifulSoup as BSoup
import os, re, sys, math, os.path, urllib, string, random, time

url = urllib.urlopen(sys.argv[1]).read()
    soup= BSoup(url)

links = []
for link in soup.findAll('a',attrs={'class':'comments may-blank'}):
    links.append(link.get("href"))

print links

我已经成功地测试了r/gaming和r/worldnews的代码,但是r/gifs的代码失败了。我还验证了所有子reddits都使用了同一个类。加上我试过

^{pr2}$

但是代码还是找不到超链接。关于为什么会发生这种情况以及如何使代码与所有子reddits一起工作的任何建议。在


Tags: 代码importurloshtmlsyslinklinks
1条回答
网友
1楼 · 发布于 2024-06-02 10:55:13

如果你经常这样做,你会遇到这样的情况。在

As a reminder to developers, we recommend that clients make no
more than <a href="http://github.com/reddit/reddit/wiki/API">one
request every two seconds</a> to avoid seeing this message.

Reddit这样做是为了防止蜘蛛和爬虫的滥用。在

或者为请求留出空间,或者更好地使用它们的Python api PRAW

相关问题 更多 >