【问题标题】:Python - Reddit web crawler using BeautifulSoup4 returns nothingPython - 使用 BeautifulSoup4 的 Reddit 网络爬虫不返回任何内容
【发布时间】:2024-07-02 20:30:01
【问题描述】:

我尝试为 Reddit 的 /r/all 创建一个网络爬虫,用于收集热门帖子的链接。我一直在 YouTube 上关注thenewboston's web crawler tutorial series 的第一部分。

在我的代码中,我删除了 while 循环,该循环在 thenewboston 的情况下设置了要抓取的页面数量的限制(我只是去爬取 /r/all 的前 25 个帖子,只有一页)。当然,我已经进行了这些更改以适应我的网络爬虫的目的。

在我的代码中,我已将 URL 变量更改为“http://www.reddit.com/r/all/”(原因很明显),将 Soup.findAll 可迭代更改为 Soup.findAll('a', {'class': 'title may-blank loggedin'})title may-blank loggedin 是帖子标题的类在 Reddit 上)。

这是我的代码:

import requests
from bs4 import BeautifulSoup

def redditSpider():
    URL = 'http://www.reddit.com/r/all/'
    sourceCode = requests.get(URL)
    plainText = sourceCode.text
    Soup = BeautifulSoup(plainText)
    for link in Soup.findAll('a', {'class': 'title may-blank loggedin'}):
        href = 'http://www.reddit.com/r/all/' + link.get('href')
        print(href)

redditSpider()

我在每行之间使用print 语句进行了一些业余错误检查,似乎没有执行 for 循环。

要了解或比较 thenewboston 的代码和我的代码,请跳到他的迷你系列的第二部分,并在他的视频中找到显示他的代码的位置。

编辑: thenewboston 应要求提供的代码:

import requests
from bs4 import BeautifulSoup

def trade_spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'https://buckysroom.org/trade/search.php?page=' + str(page)
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)
        for link in Soup.findAll('a', {'class': 'item-name'}):
            href = 'http://buckysroom.org' + link.get('href')
            print(href)
        page += 1

trade_spider()

【问题讨论】:

    标签: python for-loop beautifulsoup web-crawler reddit


    【解决方案1】:

    这并不完全是对您问题的直接回答,但我想我会让您知道有一个为 Python 制作的 Reddit API,称为 PRAW(The Python Reddit Api Wrapper),您可能希望将其查看为它可以更轻松地完成您想做的事情。

    链接:https://praw.readthedocs.org/en/v2.1.20/

    【讨论】:

      【解决方案2】:

      首先,newboston 似乎是一个截屏视频,因此将代码放入其中会很有帮助。

      其次,我建议在本地输出文件,以便您可以在浏览器中打开它并在 Web 工具中查看您想要的内容。我还建议使用 ipython 在本地处理文件上的 BeautfulSoup,而不是每次都抓取它。

      如果你把它扔进去,你可以做到这一点:

      plainText = sourceCode.text
      f = open('something.html', 'w')
      f.write(sourceCode.text.encode('utf8'))
      

      当我运行您的代码时,首先我不得不等待,因为有几次它给了我一个我经常请求的错误页面。这可能是您的第一个问题。

      当我得到这个页面时,有很多链接,但没有你的课程。如果不看整个 Youtube 系列,我不确定“title may-blank loggedin”应该代表什么。

      现在我看到了问题

      是登录类,你没有用你的爬虫登录。

      您不必为了查看 /r/all 而登录,只需使用它即可:

      soup.findAll('a', {'class': 'title may-blank '})
      

      【讨论】:

      • 你能给我指出一个来源,指导我如何使用我的刮刀登录 reddit 吗?
      • 我更新了答案以包括我为修复它所做的代码更改。但是,我收到有关太多请求的错误消息,您需要处理很多。因此,虽然我认为我的解决方案将解决您当前的问题,但如果您更关心抓取 Reddit 而不是学习网络抓取工具,稻草人的建议会更好。
      【解决方案3】:

      您没有“登录”,因此永远不会应用该类样式。这在没有登录的情况下工作:

      import requests
      from bs4 import BeautifulSoup
      
      def redditSpider():
          URL = 'http://www.reddit.com/r/all'
          source = requests.get(URL)
          Soup = BeautifulSoup(source.text)
          for link in Soup.findAll('a',attrs={'class' : 'title may-blank '}):
              href = 'http://www.reddit.com/r/all/' + link.get('href')
              print(href)
      
      redditSpider()
      

      【讨论】:

        最近更新 更多