如何使用 BeautifulSoup 获取 vbulletin 中的最新帖子

How to get latest posts in vbulletin with BeautifulSoup

我正在尝试从该网站获取 vbulletin hack 的最新帖子:

http://ashiyane.org/forums/

这是我的尝试:

#! /usr/bin/python
# -*- encoding: utf -*- 

import urllib
from bs4 import BeautifulSoup

url = 'http://ashiyane.org/forums/'
connection = urllib.urlopen(url)
data = connection.read()
connection.close()

    # End the connection

data_html = BeautifulSoup(data, 'html.parser')
data_area = data_html.find(id='vietvbb_topstats_t_content')
title_rows = data_area.find('span', 'topx-content-tab')

for link in title_rows.find_all('a'):
    print(link.get('href'))


# testing the <a and href tag ...
# for link in soup.find_all('a'):
    #print(link.get('href'))

for 来自:

http://www.crummy.com/software/BeautifulSoup/bs4/doc/

但在 运行 之后,结果如下:

showthread.php?148716-something&s=6d9b5c706c6c6f76599018eaf55a60a5&goto=newpost

并且它是returns这15个结果的值之一。

那么,怎么了?

当你得到 title_rows 时,你得到的只是一行而不是一组 rows.So 你可以使用下面的代码,它根据你的要求返回 15 个结果。

import urllib
from bs4 import BeautifulSoup

url = 'http://ashiyane.org/forums/'
connection = urllib.urlopen(url)
data = connection.read()
connection.close()

    # End the connection

data_html = BeautifulSoup(data, 'html.parser')
data_area = data_html.find(id='vietvbb_topstats_t_content')
#title_rows = data_area.find('span', 'topx-content-tab')
title_rows = data_area.findAll("span", { "class" : "topx-content-tab" })


for link in title_rows:
    print link.find_all("a")