使用BeautifulSoup爬蟲程序獲取百度搜索結(jié)果的標(biāo)題和url示例
熟悉Java的jsoup包的話,對(duì)于Python的BeautifulSoup庫(kù)應(yīng)該很容易上手。
#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 "-----標(biāo)題----\n" + a_click.renderContents()#標(biāo)題
print "----鏈接----\n" + str(a_click.get("href"))#鏈接
print "----描述----\n" + result_table.find("div", {"class": "c-abstract"}).renderContents()#描述
print
相關(guān)文章
Python基礎(chǔ)之python循環(huán)控制語(yǔ)句break/continue詳解
Python中提供了兩個(gè)關(guān)鍵字用來(lái)控制循環(huán)語(yǔ)句,分別是break和continue,接下來(lái)通過(guò)兩個(gè)案例來(lái)區(qū)分這兩個(gè)控制語(yǔ)句的不同,感興趣的朋友一起看看吧2021-09-09python利用微信公眾號(hào)實(shí)現(xiàn)報(bào)警功能
微信公眾號(hào)共有三種,服務(wù)號(hào)、訂閱號(hào)、企業(yè)號(hào)。它們?cè)讷@取AccessToken上各有不同。接下來(lái)通過(guò)本文給大家介紹python利用微信公眾號(hào)實(shí)現(xiàn)報(bào)警功能,感興趣的朋友一起看看吧2018-06-06