美文网首页
PW05格言网的数据采集

PW05格言网的数据采集

作者: 冬季男孩 | 来源:发表于2018-07-03 21:24 被阅读0次

    实验目标

    格言网的数据进行采集 

    实验过程

    1 确定爬虫网站

    首先确定好我们要爬取的网站,并利用开发者工具查看该网站源码,可以利用得到的工具有:Safari浏览器自带的开发者工具和Goggle浏览器的插件(目前我自己知道的,这里我用到的是Safari浏览器,如图1:

    格言网源代码

    可以看到格言网的源代码十分规则,这也让我们在爬取数据的时候更加的便利:每一条格言在span中class=text下,而作者在class=auther和标签存储在class=tags下。

    2 编写爬虫代码

    在确定我们要爬的网站后,就开始写代码了,首先我们要在终端输入:

    scrapy startproject Motto

    创建了一个名叫Motto的文件夹,在文件夹中的items.py中编写一个规则,即我们需要获取的信息,这里我的代码是:

    Motto = Field() #名言

    Auther = Field() #名人

    Tags = Field() #标签

    于是我们开始在spiders文件夹下面创建一个新的Python文件,并在里面写上我们的爬虫代码:

    import scrapy

    from Motto.items import MottoItem

    class Mottospider(scrapy.Spider):

    name = 'Motto' #这个地方是我们在启动爬虫运行的名字

    allowed_domains = ["quotes.toscrape.com"] #我们要爬取的网站

    start_urls = ['http://quotes.toscrape.com/page/1'] #网站第一页

    def parse(self,response):

        for motto1 in response.xpath("//div[@class='quote']"):

            item = MottoItem()

            item['Motto'] = motto1.xpath("span[1]/text()").extract()[0].strip()

            item['Auther'] = motto1.xpath("span[2]/small/text()").extract()[0].strip()

            item['Tags'] = motto1.xpath("div/meta/@content").extract()[0].strip()

            yield item

            next_page = response.xpath('//li[@class="next"]/a/@href')

            if next_page:

               url = response.urljoin(next_page[0].extract())

                yield scrapy.Request(url,self.parse)

                  #scrapy crawl Motto -o items.json -t json

    由于我们最后保存的json格式,在这里我们要在setting.py中加上FEED_EXPORT_ENCODING = 'utf-8'这样一句话,以免出现json代码。

    3 运行爬虫代码

    一切准备就绪,这个时候我们就可以开始运行我们的代码了。在终端输入

    scrapy crawl Motto -o Motto.json

    运行后,我们可以在终端发现:

    运行结果

    代码成功运行。

    4 运行结果的检验

    我们可以在Motto文件里发现生成了一个Motto.json的文件,这就是我们上一句话起了作用,里面的数据就是我们从网站上爬取下来的数据。

    实验结果

    Motto.json:

    [

    {"Motto": "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”", "Auther": "Albert Einstein", "Tags": "change,deep-thoughts,thinking,world"},

    {"Motto": "“It is our choices, Harry, that show what we truly are, far more than our abilities.”", "Auther": "J.K. Rowling", "Tags": "abilities,choices"},

    {"Motto": "“There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.”", "Auther": "Albert Einstein", "Tags": "inspirational,life,live,miracle,miracles"},

    {"Motto": "“The person, be it gentleman or lady, who has not pleasure in a good novel, must be intolerably stupid.”", "Auther": "Jane Austen", "Tags": "aliteracy,books,classic,humor"},

    {"Motto": "“Imperfection is beauty, madness is genius and it's better to be absolutely ridiculous than absolutely boring.”", "Auther": "Marilyn Monroe", "Tags": "be-yourself,inspirational"},

    {"Motto": "“Try not to become a man of success. Rather become a man of value.”", "Auther": "Albert Einstein", "Tags": "adulthood,success,value"},

    {"Motto": "“It is better to be hated for what you are than to be loved for what you are not.”", "Auther": "André Gide", "Tags": "life,love"},

    {"Motto": "“I have not failed. I've just found 10,000 ways that won't work.”", "Auther": "Thomas A. Edison", "Tags": "edison,failure,inspirational,paraphrased"},

    {"Motto": "“A woman is like a tea bag; you never know how strong it is until it's in hot water.”", "Auther": "Eleanor Roosevelt", "Tags": "misattributed-eleanor-roosevelt"},

    {"Motto": "“A day without sunshine is like, you know, night.”", "Auther": "Steve Martin", "Tags": "humor,obvious,simile"},

    {"Motto": "“This life is what you make it. No matter what, you're going to mess up sometimes, it's a universal truth. But the good part is you get to decide how you're going to mess it up. Girls will be your friends - they'll act like it anyway. But just remember, some come, some go. The ones that stay with you through everything - they're your true best friends. Don't let go of them. Also remember, sisters make the best friends in the world. As for lovers, well, they'll come and go too. And baby, I hate to say it, most of them - actually pretty much all of them are going to break your heart, but you can't give up because if you give up, you'll never find your soulmate. You'll never find that half who makes you whole and that goes for everything. Just because you fail once, doesn't mean you're gonna fail at everything. Keep trying, hold on, and always, always, always believe in yourself, because if you don't, then who will, sweetie? So keep your head high, keep your chin up, and most importantly, keep smiling, because life's a beautiful thing and there's so much to smile about.”", "Auther": "Marilyn Monroe", "Tags": "friends,heartbreak,inspirational,life,love,sisters"},

    {"Motto": "“It takes a great deal of bravery to stand up to our enemies, but just as much to stand up to our friends.”", "Auther": "J.K. Rowling", "Tags": "courage,friends"},

    {"Motto": "“If you can't explain it to a six year old, you don't understand it yourself.”", "Auther": "Albert Einstein", "Tags": "simplicity,understand"},

    {"Motto": "“You may not be her first, her last, or her only. She loved before she may love again. But if she loves you now, what else matters? She's not perfect—you aren't either, and the two of you may never be perfect together but if she can make you laugh, cause you to think twice, and admit to being human and making mistakes, hold onto her and give her the most you can. She may not be thinking about you every second of the day, but she will give you a part of her that she knows you can break—her heart. So don't hurt her, don't change her, don't analyze and don't expect more than she can give. Smile when she makes you happy, let her know when she makes you mad, and miss her when she's not there.”", "Auther": "Bob Marley", "Tags": "love"},

    {"Motto": "“I like nonsense, it wakes up the brain cells. Fantasy is a necessary ingredient in living.”", "Auther": "Dr. Seuss", "Tags": "fantasy"},

    {"Motto": "“I may not have gone where I intended to go, but I think I have ended up where I needed to be.”", "Auther": "Douglas Adams", "Tags": "life,navigation"},

    {"Motto": "“The opposite of love is not hate, it's indifference. The opposite of art is not ugliness, it's indifference. The opposite of faith is not heresy, it's indifference. And the opposite of life is not death, it's indifference.”", "Auther": "Elie Wiesel", "Tags": "activism,apathy,hate,indifference,inspirational,love,opposite,philosophy"},

    {"Motto": "“It is not a lack of love, but a lack of friendship that makes unhappy marriages.”", "Auther": "Friedrich Nietzsche", "Tags": "friendship,lack-of-friendship,lack-of-love,love,marriage,unhappy-marriage"},

    {"Motto": "“Good friends, good books, and a sleepy conscience: this is the ideal life.”", "Auther": "Mark Twain", "Tags": "books,contentment,friends,friendship,life"},

    {"Motto": "“Life is what happens to us while we are making other plans.”", "Auther": "Allen Saunders", "Tags": "fate,life,misattributed-john-lennon,planning,plans"},

    {"Motto": "“I love you without knowing how, or when, or from where. I love you simply, without problems or pride: I love you in this way because I do not know any other way of loving but this, in which there is no I or you, so intimate that your hand upon my chest is my hand, so intimate that when I fall asleep your eyes close.”", "Auther": "Pablo Neruda", "Tags": "love,poetry"},

    {"Motto": "“For every minute you are angry you lose sixty seconds of happiness.”", "Auther": "Ralph Waldo Emerson", "Tags": "happiness"},

    {"Motto": "“If you judge people, you have no time to love them.”", "Auther": "Mother Teresa", "Tags": "attributed-no-source"},

    {"Motto": "“Anyone who thinks sitting in church can make you a Christian must also think that sitting in a garage can make you a car.”", "Auther": "Garrison Keillor", "Tags": "humor,religion"},

    {"Motto": "“Beauty is in the eye of the beholder and it may be necessary from time to time to give a stupid or misinformed beholder a black eye.”", "Auther": "Jim Henson", "Tags": "humor"},

    {"Motto": "“Today you are You, that is truer than true. There is no one alive who is Youer than You.”", "Auther": "Dr. Seuss", "Tags": "comedy,life,yourself"},

    {"Motto": "“If you want your children to be intelligent, read them fairy tales. If you want them to be more intelligent, read them more fairy tales.”", "Auther": "Albert Einstein", "Tags": "children,fairy-tales"},

    {"Motto": "“It is impossible to live without failing at something, unless you live so cautiously that you might as well not have lived at all - in which case, you fail by default.”", "Auther": "J.K. Rowling", "Tags": ""},

    {"Motto": "“Logic will get you from A to Z; imagination will get you everywhere.”", "Auther": "Albert Einstein", "Tags": "imagination"},

    {"Motto": "“One good thing about music, when it hits you, you feel no pain.”", "Auther": "Bob Marley", "Tags": "music"},

    {"Motto": "“The more that you read, the more things you will know. The more that you learn, the more places you'll go.”", "Auther": "Dr. Seuss", "Tags": "learning,reading,seuss"},

    {"Motto": "“Of course it is happening inside your head, Harry, but why on earth should that mean that it is not real?”", "Auther": "J.K. Rowling", "Tags": "dumbledore"},

    {"Motto": "“The truth is, everyone is going to hurt you. You just got to find the ones worth suffering for.”", "Auther": "Bob Marley", "Tags": "friendship"},

    {"Motto": "“Not all of us can do great things. But we can do small things with great love.”", "Auther": "Mother Teresa", "Tags": "misattributed-to-mother-teresa,paraphrased"},

    {"Motto": "“To the well-organized mind, death is but the next great adventure.”", "Auther": "J.K. Rowling", "Tags": "death,inspirational"},

    {"Motto": "“All you need is love. But a little chocolate now and then doesn't hurt.”", "Auther": "Charles M. Schulz", "Tags": "chocolate,food,humor"},

    {"Motto": "“We read to know we're not alone.”", "Auther": "William Nicholson", "Tags": "misattributed-to-c-s-lewis,reading"},

    {"Motto": "“Any fool can know. The point is to understand.”", "Auther": "Albert Einstein", "Tags": "knowledge,learning,understanding,wisdom"},

    {"Motto": "“I have always imagined that Paradise will be a kind of library.”", "Auther": "Jorge Luis Borges", "Tags": "books,library"},

    {"Motto": "“It is never too late to be what you might have been.”", "Auther": "George Eliot", "Tags": "inspirational"},

    {"Motto": "“A reader lives a thousand lives before he dies, said Jojen. The man who never reads lives only one.”", "Auther": "George R.R. Martin", "Tags": "read,readers,reading,reading-books"},

    {"Motto": "“You can never get a cup of tea large enough or a book long enough to suit me.”", "Auther": "C.S. Lewis", "Tags": "books,inspirational,reading,tea"},

    {"Motto": "“You believe lies so you eventually learn to trust no one but yourself.”", "Auther": "Marilyn Monroe", "Tags": ""},

    {"Motto": "“If you can make a woman laugh, you can make her do anything.”", "Auther": "Marilyn Monroe", "Tags": "girls,love"},

    {"Motto": "“Life is like riding a bicycle. To keep your balance, you must keep moving.”", "Auther": "Albert Einstein", "Tags": "life,simile"},

    {"Motto": "“The real lover is the man who can thrill you by kissing your forehead or smiling into your eyes or just staring into space.”", "Auther": "Marilyn Monroe", "Tags": "love"},

    {"Motto": "“A wise girl kisses but doesn't love, listens but doesn't believe, and leaves before she is left.”", "Auther": "Marilyn Monroe", "Tags": "attributed-no-source"},

    {"Motto": "“Only in the darkness can you see the stars.”", "Auther": "Martin Luther King Jr.", "Tags": "hope,inspirational"},

    {"Motto": "“It matters not what someone is born, but what they grow to be.”", "Auther": "J.K. Rowling", "Tags": "dumbledore"},

    {"Motto": "“Love does not begin and end the way we seem to think it does. Love is a battle, love is a war; love is a growing up.”", "Auther": "James Baldwin", "Tags": "love"},

    {"Motto": "“There is nothing I would not do for those who are really my friends. I have no notion of loving people by halves, it is not my nature.”", "Auther": "Jane Austen", "Tags": "friendship,love"},

    {"Motto": "“Do one thing every day that scares you.”", "Auther": "Eleanor Roosevelt", "Tags": "attributed,fear,inspiration"},

    {"Motto": "“I am good, but not an angel. I do sin, but I am not the devil. I am just a small girl in a big world trying to find someone to love.”", "Auther": "Marilyn Monroe", "Tags": "attributed-no-source"},

    {"Motto": "“If I were not a physicist, I would probably be a musician. I often think in music. I live my daydreams in music. I see my life in terms of music.”", "Auther": "Albert Einstein", "Tags": "music"},

    {"Motto": "“If you only read the books that everyone else is reading, you can only think what everyone else is thinking.”", "Auther": "Haruki Murakami", "Tags": "books,thought"},

    {"Motto": "“The difference between genius and stupidity is: genius has its limits.”", "Auther": "Alexandre Dumas fils", "Tags": "misattributed-to-einstein"},

    {"Motto": "“He's like a drug for you, Bella.”", "Auther": "Stephenie Meyer", "Tags": "drug,romance,simile"},

    {"Motto": "“There is no friend as loyal as a book.”", "Auther": "Ernest Hemingway", "Tags": "books,friends,novelist-quotes"},

    {"Motto": "“When one door of happiness closes, another opens; but often we look so long at the closed door that we do not see the one which has been opened for us.”", "Auther": "Helen Keller", "Tags": "inspirational"},

    {"Motto": "“Life isn't about finding yourself. Life is about creating yourself.”", "Auther": "George Bernard Shaw", "Tags": "inspirational,life,yourself"},

    {"Motto": "“That's the problem with drinking, I thought, as I poured myself a drink. If something bad happens you drink in an attempt to forget; if something good happens you drink in order to celebrate; and if nothing happens you drink to make something happen.”", "Auther": "Charles Bukowski", "Tags": "alcohol"},

    {"Motto": "“You don’t forget the face of the person who was your last hope.”", "Auther": "Suzanne Collins", "Tags": "the-hunger-games"},

    {"Motto": "“Remember, we're madly in love, so it's all right to kiss me anytime you feel like it.”", "Auther": "Suzanne Collins", "Tags": "humor"},

    {"Motto": "“To love at all is to be vulnerable. Love anything and your heart will be wrung and possibly broken. If you want to make sure of keeping it intact you must give it to no one, not even an animal. Wrap it carefully round with hobbies and little luxuries; avoid all entanglements. Lock it up safe in the casket or coffin of your selfishness. But in that casket, safe, dark, motionless, airless, it will change. It will not be broken; it will become unbreakable, impenetrable, irredeemable. To love is to be vulnerable.”", "Auther": "C.S. Lewis", "Tags": "love"},

    {"Motto": "“Not all those who wander are lost.”", "Auther": "J.R.R. Tolkien", "Tags": "bilbo,journey,lost,quest,travel,wander"},

    {"Motto": "“Do not pity the dead, Harry. Pity the living, and, above all those who live without love.”", "Auther": "J.K. Rowling", "Tags": "live-death-love"},

    {"Motto": "“There is nothing to writing. All you do is sit down at a typewriter and bleed.”", "Auther": "Ernest Hemingway", "Tags": "good,writing"},

    {"Motto": "“Finish each day and be done with it. You have done what you could. Some blunders and absurdities no doubt crept in; forget them as soon as you can. Tomorrow is a new day. You shall begin it serenely and with too high a spirit to be encumbered with your old nonsense.”", "Auther": "Ralph Waldo Emerson", "Tags": "life,regrets"},

    {"Motto": "“I have never let my schooling interfere with my education.”", "Auther": "Mark Twain", "Tags": "education"},

    {"Motto": "“I have heard there are troubles of more than one kind. Some come from ahead and some come from behind. But I've bought a big bat. I'm all ready you see. Now my troubles are going to have troubles with me!”", "Auther": "Dr. Seuss", "Tags": "troubles"},

    {"Motto": "“If I had a flower for every time I thought of you...I could walk through my garden forever.”", "Auther": "Alfred Tennyson", "Tags": "friendship,love"},

    {"Motto": "“Some people never go crazy. What truly horrible lives they must lead.”", "Auther": "Charles Bukowski", "Tags": "humor"},

    {"Motto": "“The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.”", "Auther": "Terry Pratchett", "Tags": "humor,open-mind,thinking"},

    {"Motto": "“Think left and think right and think low and think high. Oh, the thinks you can think up if only you try!”", "Auther": "Dr. Seuss", "Tags": "humor,philosophy"},

    {"Motto": "“What really knocks me out is a book that, when you're all done reading it, you wish the author that wrote it was a terrific friend of yours and you could call him up on the phone whenever you felt like it. That doesn't happen much, though.”", "Auther": "J.D. Salinger", "Tags": "authors,books,literature,reading,writing"},

    {"Motto": "“The reason I talk to myself is because I’m the only one whose answers I accept.”", "Auther": "George Carlin", "Tags": "humor,insanity,lies,lying,self-indulgence,truth"},

    {"Motto": "“You may say I'm a dreamer, but I'm not the only one. I hope someday you'll join us. And the world will live as one.”", "Auther": "John Lennon", "Tags": "beatles,connection,dreamers,dreaming,dreams,hope,inspirational,peace"},

    {"Motto": "“I am free of all prejudice. I hate everyone equally. ”", "Auther": "W.C. Fields", "Tags": "humor,sinister"},

    {"Motto": "“The question isn't who is going to let me; it's who is going to stop me.”", "Auther": "Ayn Rand", "Tags": ""},

    {"Motto": "“′Classic′ - a book which people praise and don't read.”", "Auther": "Mark Twain", "Tags": "books,classic,reading"},

    {"Motto": "“Anyone who has never made a mistake has never tried anything new.”", "Auther": "Albert Einstein", "Tags": "mistakes"},

    {"Motto": "“A lady's imagination is very rapid; it jumps from admiration to love, from love to matrimony in a moment.”", "Auther": "Jane Austen", "Tags": "humor,love,romantic,women"},

    {"Motto": "“Remember, if the time should come when you have to make a choice between what is right and what is easy, remember what happened to a boy who was good, and kind, and brave, because he strayed across the path of Lord Voldemort. Remember Cedric Diggory.”", "Auther": "J.K. Rowling", "Tags": "integrity"},

    {"Motto": "“I declare after all there is no enjoyment like reading! How much sooner one tires of any thing than of a book! -- When I have a house of my own, I shall be miserable if I have not an excellent library.”", "Auther": "Jane Austen", "Tags": "books,library,reading"},

    {"Motto": "“There are few people whom I really love, and still fewer of whom I think well. The more I see of the world, the more am I dissatisfied with it; and every day confirms my belief of the inconsistency of all human characters, and of the little dependence that can be placed on the appearance of merit or sense.”", "Auther": "Jane Austen", "Tags": "elizabeth-bennet,jane-austen"},

    {"Motto": "“Some day you will be old enough to start reading fairy tales again.”", "Auther": "C.S. Lewis", "Tags": "age,fairytales,growing-up"},

    {"Motto": "“We are not necessarily doubting that God will do the best for us; we are wondering how painful the best will turn out to be.”", "Auther": "C.S. Lewis", "Tags": "god"},

    {"Motto": "“The fear of death follows from the fear of life. A man who lives fully is prepared to die at any time.”", "Auther": "Mark Twain", "Tags": "death,life"},

    {"Motto": "“A lie can travel half way around the world while the truth is putting on its shoes.”", "Auther": "Mark Twain", "Tags": "misattributed-mark-twain,truth"},

    {"Motto": "“I believe in Christianity as I believe that the sun has risen: not only because I see it, but because by it I see everything else.”", "Auther": "C.S. Lewis", "Tags": "christianity,faith,religion,sun"},

    {"Motto": "“The truth.\" Dumbledore sighed. \"It is a beautiful and terrible thing, and should therefore be treated with great caution.”", "Auther": "J.K. Rowling", "Tags": "truth"},

    {"Motto": "“I'm the one that's got to die when it's time for me to die, so let me live my life the way I want to.”", "Auther": "Jimi Hendrix", "Tags": "death,life"},

    {"Motto": "“To die will be an awfully big adventure.”", "Auther": "J.M. Barrie", "Tags": "adventure,love"},

    {"Motto": "“It takes courage to grow up and become who you really are.”", "Auther": "E.E. Cummings", "Tags": "courage"},

    {"Motto": "“But better to get hurt by the truth than comforted with a lie.”", "Auther": "Khaled Hosseini", "Tags": "life"},

    {"Motto": "“You never really understand a person until you consider things from his point of view... Until you climb inside of his skin and walk around in it.”", "Auther": "Harper Lee", "Tags": "better-life-empathy"},

    {"Motto": "“You have to write the book that wants to be written. And if the book will be too difficult for grown-ups, then you write it for children.”", "Auther": "Madeleine L'Engle", "Tags": "books,children,difficult,grown-ups,write,writers,writing"},

    {"Motto": "“Never tell the truth to people who are not worthy of it.”", "Auther": "Mark Twain", "Tags": "truth"},

    {"Motto": "“A person's a person, no matter how small.”", "Auther": "Dr. Seuss", "Tags": "inspirational"},

    {"Motto": "“... a mind needs books as a sword needs a whetstone, if it is to keep its edge.”", "Auther": "George R.R. Martin", "Tags": "books,mind"}

    ]

    到此为止,谢谢观看。


    相关文章

      网友评论

          本文标题:PW05格言网的数据采集

          本文链接:https://www.haomeiwen.com/subject/qpgjuftx.html