【发布时间】: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 中的网络爬虫,用于下载所有带有论坛帖子的网页并将其传输到 xml。 github.com/vizzerdrix55/web-scraping-vBulletin-forumgithub.com/vizzerdrix55/web-scraping-vBulletin-forum/commit/…github.com/GregUK/vb3_7_scrapervb3_7_scraper VBulletin 论坛爬虫使用 python。这是为 www.s2forum.com 量身定制的,蜘蛛可以很容易地为其他网站更新 尝试使用 python scrapey 到额外的论坛、线程和来自 VBulletin 论坛 pre API 的帖子
标签: python web-scraping pycharm nested-loops