【发布时间】:2017-02-23 17:23:06
【问题描述】:
我正在尝试从http://pantip.com/tag/Isuzu 抓取一些论坛帖子
一个这样的页面是http://pantip.com/topic/35647305
我想将每篇文章连同其作者和时间戳一起保存到一个 csv 文件中。
我正在使用 Beautiful Soup,但不可否认,我是 python 和网络抓取的初学者。我现在拥有的代码获得了必填字段,但仅适用于第一篇文章。我需要该线程上所有帖子的信息。我尝试了 soup.find_all() 和 soup.select(),但没有得到想要的结果。
这是我正在使用的代码:
from bs4 import BeautifulSoup
import urllib2
print "Reading URL..."
url = urllib2.urlopen("http://pantip.com/topic/35647305")
content = url.read()
soup = BeautifulSoup(content, "html.parser")
print "Finding desired HTML..."
table = soup.select("abbr.timeago")
print "\nScraped HTML is:"
print table
text = BeautifulSoup(str(table).strip(),"html.parser").get_text().encode("utf-8").replace("\n", "")
print "\nScraped text is:\n" + text
任何关于我做错了什么的线索将不胜感激。此外,欢迎任何关于如何以更好、更清洁的方式完成此操作的建议。
如前所述,我是初学者,所以请不要介意任何愚蠢的错误。 :-)
谢谢!
【问题讨论】:
-
我认为
selenium将是解决这个问题的方法。由于很多 html 似乎是用 javascript 生成的。
标签: python html web-scraping beautifulsoup