Python爬蟲包BeautifulSoup學(xué)習(xí)實例(五)
本文為大家分享了Python爬蟲包BeautifulSoup學(xué)習(xí)實例,具體內(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"}) 等價寫法
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實現(xiàn)上述功能
這里有一些HTMLParser的基礎(chǔ)教程
由于HtmlParser自2006年以后就再沒更新,目前很多人推薦使用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=(',', ': ')))
以上全部為本篇文章的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python爬蟲庫BeautifulSoup獲取對象(標簽)名,屬性,內(nèi)容,注釋
- Python爬蟲庫BeautifulSoup的介紹與簡單使用實例
- Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例
- Python爬蟲beautifulsoup4常用的解析方法總結(jié)
- Python爬蟲包BeautifulSoup實例(三)
- Python爬蟲包BeautifulSoup異常處理(二)
- Python爬蟲包BeautifulSoup簡介與安裝(一)
- python爬蟲之BeautifulSoup 使用select方法詳解
- python爬蟲入門教程--HTML文本的解析庫BeautifulSoup(四)
- Python爬蟲包 BeautifulSoup 遞歸抓取實例詳解
- 使用Python爬蟲庫BeautifulSoup遍歷文檔樹并對標簽進行操作詳解
相關(guān)文章
Python+flask實現(xiàn)restful接口的示例詳解
這篇文章主要為大家詳細介紹了Python如何利用flask實現(xiàn)restful接口,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2023-02-02
Selenium+Python自動化腳本環(huán)境搭建的全過程
說到自動化測試,就不得不提大名鼎鼎的Selenium,Selenium 是如今最常用的自動化測試工具之一,支持快速開發(fā)自動化測試框架,且支持在多種瀏覽器上執(zhí)行測試,下面這篇文章主要給大家介紹了關(guān)于Selenium+Python自動化腳本環(huán)境搭建的相關(guān)資料,需要的朋友可以參考下2021-09-09
python中的循環(huán)結(jié)構(gòu)問題
這篇文章主要介紹了python中的循環(huán)結(jié)構(gòu)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實例
今天小編就為大家分享一篇tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
python判斷文件夾內(nèi)是否存在指定后綴文件的實例
今天小編就為大家分享一篇python判斷文件夾內(nèi)是否存在指定后綴文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python字典刪除鍵值對和元素的四種方法(小結(jié))
刪除列表或者字符串元素的方法不止一種,同樣,刪除字典元素的方法也不止一種,本文主要介紹python中刪除字典元素的四種方法:1、使用del語句;2、使用clear();3、使用pop();4、使用popitem()。感興趣的可以了解一下2021-12-12
Python 比較文本相似性的方法(difflib,Levenshtein)
今天小編就為大家分享一篇Python 比較文本相似性的方法(difflib,Levenshtein),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10

