熟悉Java的jsoup包的話,對于Python的BeautifulSoup庫應該很容易上手。
代碼如下:
#coding: utf-8
import sys
import urllib
import urllib2
from BeautifulSoup import BeautifulSoup
question_word = "吃貨 程序員"
url = "http://www.baidu.com/s?wd=" + urllib.quote(question_word.decode(sys.stdin.encoding).encode('gbk'))
htmlpage = urllib2.urlopen(url).read()
soup = BeautifulSoup(htmlpage)
print len(soup.findAll("table", {"class": "result"}))
for result_table in soup.findAll("table", {"class": "result"}):
a_click = result_table.find("a")
print "-----標題----/n" + a_click.renderContents()#標題
print "----鏈接----/n" + str(a_click.get("href"))#鏈接
print "----描述----/n" + result_table.find("div", {"class": "c-abstract"}).renderContents()#描述
print
新聞熱點
疑難解答