Python爬虫程序获取百度搜索结果的标题、描述、url。,python爬虫,使用BeautifulS


使用BeautifulSoup编写了一段爬虫程序。熟悉Java的jsoup包的话,对于Python的BeautifulSoup库应该很容易上手。

#coding: utf-8import sysimport urllibimport urllib2from BeautifulSoup import BeautifulSoupquestion_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#该片段来自于http://byrx.net

评论关闭