使用BeautifulSoup爬蟲程序獲取百度搜索結(jié)果的標題和url示例
熟悉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
相關(guān)文章
Python圖像的增強處理操作示例【基于ImageEnhance類】
這篇文章主要介紹了Python圖像的增強處理操作,結(jié)合實例形式分析了使用ImageEnhance類處理圖片的亮度、對比度、色度以及銳度等相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
Python不同目錄間進行模塊調(diào)用的實現(xiàn)方法
這篇文章主要介紹了Python不同目錄間進行模塊調(diào)用的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01

