Python论坛:网络刮擦每个论坛帖子(Python Beautifulsoup)

在其他 stackoverflow 成员的帮助下,设法通过某些主题的所有页面进行挖掘,收集每个帖子的日期、标题和链接。

我也有一个单独的脚本,我现在正在努力实施(对于发现的每个链接,python 为它创建一个新的汤,刮擦所有的帖子,然后回到以前的链接)。

真的很感激任何其他提示或建议如何做得更好,因为这是我第一次使用 python,我认为这可能是我的嵌套循环逻辑搞砸了,但通过多次检查似乎对我来说是正确的。

下面是代码片段:

        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

因此,它似乎确实找到了新页面的正确链接并将其刮掉,但是对于下一个 itteration,它将打印新日期和相同的确切页面。在打印最后一个链接后,还有一个非常奇怪的 10-12 秒延迟,然后它跳下打印数字 1,然后剔除所有新日期。

但是在下一个论坛线程链接之后,它每次都会抓取相同的确切数据。

对不起,如果它看起来真的很乱,这是一个侧面项目,我第一次尝试做一些有用的事情,所以我很新,任何建议或提示将不胜感激。

2

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

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

    link += (div.get('href'))
    savedData += "\n" + title + ", " + link
    soup3 = make_soup('http://www.automotiveforums.com/vbulletin/' + link)
    while tempNumber < 4:
        for postSe in soup3.find_all(id=re.compile("^td_post_")):
            post = ""
            post += postSe.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 创建另一个问题的解决方案:

1

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

因为你只提供一个片段,我只是试图提供一种方法,显示如何登录到 phpBB-使用有效载荷:

import requests
forum = "the forum name"
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username': 'username', 'pword': 'pword', 'redirect':'index.php', 'sid':'', 'login':'Login'}
session = requests.Session()
r = session.post(forum + "ucp.php?mode=login", headers=headers, data=payload)
print(r.text)

但等待:我们可以-而不是使用请求来操纵网站,也可以使用浏览器自动化,如机械化提供这个,这样我们就不必管理自己的会话,只有几行代码来制作每个请求。

一个有趣的例子是在 GitHubhttps://github.com/winny-/sirsi/blob/317928f23847f4fe85e2428598fbe44c4dae2352/sirsi/sirsi.py#L74-L211

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(533)
Catia草图约束:CATIA中的 VBA编程
上一篇
Kan s a:看板 /Scrum板(kan board)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(22条)