【问题标题】:Web scraping every forum post (Python, Beautifulsoup)网络抓取每个论坛帖子(Python、Beautifulsoup)
【发布时间】:2017-07-22 06:37:50
【问题描述】:

大家好,各位堆友们。简短描述.. 我正在使用 Python 从汽车论坛中抓取一些数据并将所有数据保存到 CSV 文件中。在其他 stackoverflow 成员的帮助下,设法挖掘特定主题的所有页面,收集每个帖子的日期、标题和链接。

我还有一个单独的脚本,我现在正在努力实现(对于找到的每个链接,python 都会为其创建一个新汤,浏览所有帖子,然后返回到上一个链接)。

非常感谢任何其他关于如何使它变得更好的提示或建议,因为这是我第一次使用 python,我认为这可能是我的嵌套循环逻辑搞砸了,但多次检查对我来说似乎是正确的。

代码如下:sn-p:

        link += (div.get('href'))
        savedData += "\n" + title + ", " + link
        tempSoup = make_soup('http://www.automotiveforums.com/vbulletin/' + link)
        while tempNumber < 3:
            for tempRow in tempSoup.find_all(id=re.compile("^td_post_")):
                for tempNext in tempSoup.find_all(title=re.compile("^Next Page -")):
                    tempNextPage = ""
                    tempNextPage += (tempNext.get('href'))
                post = ""
                post += tempRow.get_text(strip=True)
                postData += post + "\n"
            tempNumber += 1
            tempNewUrl = "http://www.automotiveforums.com/vbulletin/" + tempNextPage
            tempSoup = make_soup(tempNewUrl)
            print(tempNewUrl)
    tempNumber = 1
    number += 1
    print(number)
    newUrl = "http://www.automotiveforums.com/vbulletin/" + nextPage
    soup = make_soup(newUrl)

到目前为止,我的主要问题是tempSoup = make_soup('http://www.automotiveforums.com/vbulletin/' + link) 在为论坛帖子抓取所有帖子后,似乎没有创建新汤。

这是我得到的输出:

 http://www.automotiveforums.com/vbulletin/showthread.php?s=6a2caa2b46531be10e8b1c4acb848776&t=1139532&page=2
    http://www.automotiveforums.com/vbulletin/showthread.php?s=6a2caa2b46531be10e8b1c4acb848776&t=1139532&page=3
    1

所以它似乎确实为新页面找到了正确的链接并抓取它们,但是对于下一次迭代,它会打印新日期和相同的确切页面。在打印最后一个链接后,还有一个非常奇怪的 10-12 秒延迟,然后它才跳下来打印数字 1,然后删除所有新日期..

但是在寻找下一个论坛主题链接后,它每次都会抓取相同的确切数据。

对不起,如果它看起来真的很乱,这是一个副项目,我第一次尝试做一些有用的事情,所以我对此很陌生,任何建议或提示将不胜感激。我不是要你为我解决代码,即使是我可能错误的逻辑的一些指针也将不胜感激!

【问题讨论】:

标签: python web-scraping pycharm nested-loops


【解决方案1】:

非常感谢亲爱的 Norbis 分享您的想法、见解和概念

因为你只提供一个 sn-p 我只是尝试提供一种方法来显示如何登录到 phpBB - 使用有效负载:

import requests
forum = "the forum name"

headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username': 'username', 'password': 'password', 'redirect':'index.php', 'sid':'', 'login':'Login'}
session = requests.Session()

r = session.post(forum + "ucp.php?mode=login", headers=headers, data=payload)
print(r.text)

但请稍等:我们可以 - 而不是使用请求来操纵网站, 还利用浏览器自动化,如 mechanize 提供此功能。 这样我们就不必管理自己的会话,只需几行代码来制作每个请求。

GitHub 上有一个有趣的例子https://github.com/winny-/sirsi/blob/317928f23847f4fe85e2428598fbe44c4dae2352/sirsi/sirsi.py#L74-L211

【讨论】:

    【解决方案2】:

    所以在花了一点时间之后,我几乎成功地破解了它。现在是python在论坛上找到每个线程和它的链接,然后进入每个链接,阅读所有页面并继续下一个链接。

    如果有人会使用它,这是它的固定代码。

        link += (div.get('href'))
        savedData += "\n" + title + ", " + link
        soup3 = make_soup('http://www.automotiveforums.com/vbulletin/' + link)
        while tempNumber < 4:
            for postScrape in soup3.find_all(id=re.compile("^td_post_")):
                post = ""
                post += postScrape.get_text(strip=True)
                postData += post + "\n"
                print(post)
            for tempNext in soup3.find_all(title=re.compile("^Next Page -")):
                tempNextPage = ""
                tempNextPage += (tempNext.get('href'))
                print(tempNextPage)
            soup3 = ""
            soup3 = make_soup('http://www.automotiveforums.com/vbulletin/' + tempNextPage)
            tempNumber += 1
        tempNumber = 1
    number += 1
    print(number)
    newUrl = "http://www.automotiveforums.com/vbulletin/" + nextPage
    soup = make_soup(newUrl)
    

    我所要做的就是将相互嵌套的 2 个 for 循环分离成自己的循环。仍然不是一个完美的解决方案,但嘿,它几乎可以工作。

    非工作位:所提供链接的前 2 个线程有多个帖子页面。以下 10 多个线程不要。我想不出办法检查for tempNext in soup3.find_all(title=re.compile("^Next Page -")): 循环外的值以查看它是否为空。因为如果它没有找到下一页元素/href,它只会使用最后一个。但是如果我在每次运行后重置该值,它就不再挖掘每一页 =l 一个刚刚产生另一个问题的解决方案:D。

    【讨论】:

    • 你好亲爱的诺比斯 - 非常感谢这段很棒的代码 - 更好:顺便说一句:获取论坛某个用户的数据的选项怎么样 - 这可能吗?会很棒
    猜你喜欢
    • 1970-01-01
    • 2018-12-06
    • 2014-03-25
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    相关资源
    最近更新 更多