亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python爬豆瓣電影實(shí)例

 更新時(shí)間:2018年02月23日 13:56:57   投稿:laozhang  
本篇文章給大家通過Python爬豆瓣電影實(shí)例對(duì)Python爬蟲更深入的講解,有興趣的學(xué)習(xí)下。

文件結(jié)構(gòu)

html_downloader.py - 下載網(wǎng)頁(yè)html內(nèi)容

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import urllib2

class HtmlDownloader(object):

  def downlod(self, url):
    if url is None:
      return None
    response = urllib2.urlopen(url)
    if response.getcode() != 200:
      return None
    return response.read()

html_outputer.py - 輸出結(jié)果到文件中

#!/usr/bin/python
# -*- coding: UTF-8 -*-


class HtmlOutputer(object):

  def collect_data(self, movie_data):
    if movie_data is None:
      return
    fout = open('output.html', 'a+')
    for data in movie_data:
      print data['name'] + '|', data['rate'] + '|', data['actor'], '\n'
      fout.write('%s,' % data['name'].encode('utf-8'))
      fout.write('%s,' % data['rate'])
      fout.write('%s\n' % data['actor'].encode('utf-8'))
    fout.close()

html_parser.py: 解析器:解析html的dom樹

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup


class HtmlParser(object):

  def __init__(self):
    pass

  def parser_html(self, cnt):
    if cnt is None:
      return
    soup = BeautifulSoup(cnt, 'html.parser', from_encoding='utf-8')
    # movie_name, movie_desc, movie_rate =
    return self.get_movie_names(soup)

  def get_movie_names(self, soup):
    movie_data = []
    movie_all = soup.find('div', class_='article').find_next('table').find_next_sibling('div').find_next_sibling('div').find_all('table')
    count = 1
    for movie_one in movie_all:
      movie_data.append(self.get_movie_name(movie_one))
      # if count > 2:
      #   break
      count += 1
    return movie_data

  def get_movie_name(self, cnt):
    info = {}
    soup = BeautifulSoup(str(cnt), 'html.parser', from_encoding='utf-8')
    movie_one = soup.find('tr', class_='item').find_next('td').find_next_sibling('td').find('div', class_='pl2')
    info['name'] = movie_one.find('a').get_text().replace("\n", "").replace(" ", "")
    info['actor'] = movie_one.find('p', class_='pl').get_text().replace("\n", "").replace(" ", "")
    info['rate'] = movie_one.find('div', class_='star clearfix').find('span', class_='rating_nums').get_text()
    return info

spider_main.py - 主函數(shù)

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import html_parser, html_outputer, html_downloader


class SpiderMain(object):

  def __init__(self):
    self.parser = html_parser.HtmlParser()
    self.outputer = html_outputer.HtmlOutputer()
    self.downloader = html_downloader.HtmlDownloader()

  def craw(self, url):
    html_cnt = self.downloader.downlod(url)
    movie_data = self.parser.parser_html(html_cnt)
    self.outputer.collect_data(movie_data)


if __name__ == '__main__':
  url = 'https://movie.douban.com/tag/2017?start=100&type=T'
  spider = SpiderMain()
  spider.craw(url)

綜述

其實(shí)就是使用了urllib2和BeautifulSoup庫(kù),沒啥好說的,你也可以直接改url,然后更改html_parser.py文件來滿足你自己的爬蟲需求。當(dāng)前也可以更改html_outputer.py來定義保存格式,目前是csv。

相關(guān)文章

  • python串口讀取數(shù)據(jù)的實(shí)例

    python串口讀取數(shù)據(jù)的實(shí)例

    這篇文章主要介紹了python串口讀取數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python引入其他py文件或模塊

    python引入其他py文件或模塊

    本文主要介紹了python引入其他py文件或模塊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • Django如何使用第三方服務(wù)發(fā)送電子郵件

    Django如何使用第三方服務(wù)發(fā)送電子郵件

    這篇文章主要介紹了Django如何使用第三方服務(wù)發(fā)送電子郵件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Pytorch自定義Dataset和DataLoader去除不存在和空數(shù)據(jù)的操作

    Pytorch自定義Dataset和DataLoader去除不存在和空數(shù)據(jù)的操作

    這篇文章主要介紹了Pytorch自定義Dataset和DataLoader去除不存在和空數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python海龜繪圖(Turtle)應(yīng)用指南

    Python海龜繪圖(Turtle)應(yīng)用指南

    python2.6版本中后引入的一個(gè)簡(jiǎn)單的繪圖工具,叫做海龜繪圖(Turtle?Graphics)。海龜繪圖(turtle庫(kù))是python的內(nèi)部模塊,使用前導(dǎo)入即可。本文將展示三個(gè)通過海龜繪圖實(shí)現(xiàn)的小程序,快來跟隨小編一起學(xué)習(xí)吧
    2022-03-03
  • 基于Python實(shí)現(xiàn)最新房?jī)r(jià)信息的獲取

    基于Python實(shí)現(xiàn)最新房?jī)r(jià)信息的獲取

    這篇文章主要為大家介紹了如何利用Python獲取房?jī)r(jià)信息(以北京為例),整個(gè)數(shù)據(jù)獲取的信息是通過房源平臺(tái)獲取的,通過下載網(wǎng)頁(yè)元素并進(jìn)行數(shù)據(jù)提取分析完成整個(gè)過程,需要的可以參考一下
    2022-04-04
  • 詳解如何利用pandas進(jìn)行數(shù)據(jù)行轉(zhuǎn)列和列轉(zhuǎn)行

    詳解如何利用pandas進(jìn)行數(shù)據(jù)行轉(zhuǎn)列和列轉(zhuǎn)行

    這篇文章主要為大家詳細(xì)介紹了如何利用pandas進(jìn)行數(shù)據(jù)行轉(zhuǎn)列和列轉(zhuǎn)行,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2007-02-02
  • Python讀入mnist二進(jìn)制圖像文件并顯示實(shí)例

    Python讀入mnist二進(jìn)制圖像文件并顯示實(shí)例

    這篇文章主要介紹了Python讀入mnist二進(jìn)制圖像文件并顯示實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python 順時(shí)針打印矩陣的超簡(jiǎn)潔代碼

    python 順時(shí)針打印矩陣的超簡(jiǎn)潔代碼

    今天小編就為大家分享一篇python 順時(shí)針打印矩陣的超簡(jiǎn)潔代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • Python實(shí)現(xiàn)數(shù)據(jù)清洗的示例詳解

    Python實(shí)現(xiàn)數(shù)據(jù)清洗的示例詳解

    這篇文章主要通過五個(gè)示例帶大家深入了解下Python實(shí)現(xiàn)數(shù)據(jù)清洗的具體方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下
    2022-08-08

最新評(píng)論