Python爬蟲(chóng)包BeautifulSoup學(xué)習(xí)實(shí)例(五)
本文為大家分享了Python爬蟲(chóng)包BeautifulSoup學(xué)習(xí)實(shí)例,具體內(nèi)容如下
BeautifulSoup
使用BeautifulSoup抓取豆瓣電影的一些信息。
# -*- coding: utf-8 -*- # @Author: HaonanWu # @Date: 2016-12-24 16:18:01 # @Last Modified by: HaonanWu # @Last Modified time: 2016-12-24 17:25:33 import urllib2 import json from bs4 import BeautifulSoup def nowplaying_movies(url): user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36' headers = {'User-Agent':user_agent} request = urllib2.Request(url = url, headers = headers) response = urllib2.urlopen(request) soup_packetpage = BeautifulSoup(response, 'lxml') items = soup_packetpage.findAll("li", class_="list-item") # items = soup_packetpage.findAll("li", {"class" : "list-item"}) 等價(jià)寫(xiě)法 movies = [] for item in items: if item.attrs['data-category'] == 'nowplaying': movie = {} movie['title'] = item.attrs['data-title'] movie['score'] = item.attrs['data-score'] movie['director'] = item.attrs['data-director'] movie['actors'] = item.attrs['data-actors'] movies.append(movie) print('%(title)s|%(score)s|%(director)s|%(actors)s' % movie) return movies if __name__ == '__main__': url = 'https://movie.douban.com/nowplaying/beijing/' movies = nowplaying_movies(url) print('%s' % json.dumps(movies, sort_keys=True, indent=4, separators=(',', ': ')))
HTMLParser
使用HTMLParser實(shí)現(xiàn)上述功能
這里有一些HTMLParser的基礎(chǔ)教程
由于HtmlParser自2006年以后就再?zèng)]更新,目前很多人推薦使用jsoup代替它。
# -*- coding: utf-8 -*- # @Author: HaonanWu # @Date: 2016-12-24 15:57:54 # @Last Modified by: HaonanWu # @Last Modified time: 2016-12-24 17:03:27 from HTMLParser import HTMLParser import urllib2 import json class MovieParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.movies = [] def handle_starttag(self, tag, attrs): def _attr(attrlist, attrname): for attr in attrlist: if attr[0] == attrname: return attr[1] return None if tag == 'li' and _attr(attrs, 'data-title') and _attr(attrs, 'data-category') == 'nowplaying': movie = {} movie['title'] = _attr(attrs, 'data-title') movie['score'] = _attr(attrs, 'data-score') movie['director'] = _attr(attrs, 'data-director') movie['actors'] = _attr(attrs, 'data-actors') self.movies.append(movie) print('%(title)s|%(score)s|%(director)s|%(actors)s' % movie) def nowplaying_movies(url): headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'} req = urllib2.Request(url, headers=headers) s = urllib2.urlopen(req) parser = MovieParser() parser.feed(s.read()) s.close() return parser.movies if __name__ == '__main__': url = 'https://movie.douban.com/nowplaying/beijing/' movies = nowplaying_movies(url) print('%s' % json.dumps(movies, sort_keys=True, indent=4, separators=(',', ': ')))
以上全部為本篇文章的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python爬蟲(chóng)庫(kù)BeautifulSoup獲取對(duì)象(標(biāo)簽)名,屬性,內(nèi)容,注釋
- Python爬蟲(chóng)庫(kù)BeautifulSoup的介紹與簡(jiǎn)單使用實(shí)例
- Python爬蟲(chóng)實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例
- Python爬蟲(chóng)beautifulsoup4常用的解析方法總結(jié)
- Python爬蟲(chóng)包BeautifulSoup實(shí)例(三)
- Python爬蟲(chóng)包BeautifulSoup異常處理(二)
- Python爬蟲(chóng)包BeautifulSoup簡(jiǎn)介與安裝(一)
- python爬蟲(chóng)之BeautifulSoup 使用select方法詳解
- python爬蟲(chóng)入門(mén)教程--HTML文本的解析庫(kù)BeautifulSoup(四)
- Python爬蟲(chóng)包 BeautifulSoup 遞歸抓取實(shí)例詳解
- 使用Python爬蟲(chóng)庫(kù)BeautifulSoup遍歷文檔樹(shù)并對(duì)標(biāo)簽進(jìn)行操作詳解
相關(guān)文章
Python+flask實(shí)現(xiàn)restful接口的示例詳解
這篇文章主要為大家詳細(xì)介紹了Python如何利用flask實(shí)現(xiàn)restful接口,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02Selenium+Python自動(dòng)化腳本環(huán)境搭建的全過(guò)程
說(shuō)到自動(dòng)化測(cè)試,就不得不提大名鼎鼎的Selenium,Selenium 是如今最常用的自動(dòng)化測(cè)試工具之一,支持快速開(kāi)發(fā)自動(dòng)化測(cè)試框架,且支持在多種瀏覽器上執(zhí)行測(cè)試,下面這篇文章主要給大家介紹了關(guān)于Selenium+Python自動(dòng)化腳本環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下2021-09-09flask 實(shí)現(xiàn)token機(jī)制的示例代碼
這篇文章主要介紹了flask 實(shí)現(xiàn)token機(jī)制的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11python中的循環(huán)結(jié)構(gòu)問(wèn)題
這篇文章主要介紹了python中的循環(huán)結(jié)構(gòu)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實(shí)例
今天小編就為大家分享一篇tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01python 用Matplotlib作圖中有多個(gè)Y軸
這篇文章主要介紹了python 如何用Matplotlib作圖中有多個(gè)Y軸,幫助大家更好的利用python繪圖,感興趣的朋友可以了解下2020-11-11python判斷文件夾內(nèi)是否存在指定后綴文件的實(shí)例
今天小編就為大家分享一篇python判斷文件夾內(nèi)是否存在指定后綴文件的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python字典刪除鍵值對(duì)和元素的四種方法(小結(jié))
刪除列表或者字符串元素的方法不止一種,同樣,刪除字典元素的方法也不止一種,本文主要介紹python中刪除字典元素的四種方法:1、使用del語(yǔ)句;2、使用clear();3、使用pop();4、使用popitem()。感興趣的可以了解一下2021-12-12Python 比較文本相似性的方法(difflib,Levenshtein)
今天小編就為大家分享一篇Python 比較文本相似性的方法(difflib,Levenshtein),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10