2017-04-04 51 views
0

這是我的第一個問題(業餘蟒學習者)。我把它寫成了正則表達式,但是我讀了幾個網站,比如this one,這些網站討論的是美麗的緩慢。我也讀過lxml更快,我讀了它的文檔,但我仍然不知道如何(或者是否有意義)在這裏更快地交換bs4。將bs4更改爲使用正則表達式更快的東西

因此,我非常感謝一個關於如何在下面的代碼中更快換掉bs4的例子。如果我對這種情況的理解是錯誤的,請讓我知道(我還在學習)!謝謝!

''' 
This script extracts all of Portia's lines in Shakespeare's Merchant of Venice 
''' 

import re 
import urllib.request 
import bs4 

# use urllib.request.urlopen and beautifulsoup to get text of merchant of venice 
u = urllib.request.urlopen('http://shakespeare.mit.edu/merchant/full.html') 
sp = bs4.BeautifulSoup(u, 'lxml') 
s = sp.get_text() 

# extracts text which follows PORTIA\n\n, and which ends before \n\n, re.DOTALL causes '.' to include newlines 
r = re.compile(r'(?<=PORTIA\n\n)(.+?)\n\n', re.DOTALL) 

# print each match 
for i in r.findall(s): 
    print(i) 
+0

千萬不要使用正則表達式來解析HTML –

+0

我試圖解析與除了bs4以外的其他東西的HTML,然後對結果做我的正則表達式的東西。但我不知道如何。 – siowy

+0

你不是已經在使用lxml作爲解析器嗎?它對你來說真的很慢,還是你剛剛讀完了你所讀的內容?在修復沒有損壞的東西時沒有意義。 –

回答

0

您可以使用lxml模塊,是數量級比BeautifulSoup更快,如果寫不好,是這樣的:

import requests 
from lxml import html 
from datetime import datetime 

u = requests.get('http://shakespeare.mit.edu/merchant/full.html').content 
start_time = datetime.now() 
sp = html.fromstring(u) 
s = [' '.join(line.xpath('./a/text()')).strip() for line in sp.xpath('//a[./b[text() = "PORTIA"]]/following-sibling::blockquote[1]')] 
print(s) 
print(datetime.now() - start_time) 

這將導致:

['By my troth, Nerissa, my little body is aweary of this great world.', 'Good sentences and well pronounced.', "If to do were as easy as to know what were good to do, chapels had been churches and poor men's cottages princes' palaces. It is a good divine that follows his own instructions: I can easier teach twenty what were good to be done, than be one of the twenty to follow mine own teaching. The brain may devise laws for the blood, but a hot temper leaps o'er a cold decree: such a hare is madness the youth, to skip o'er the meshes of good counsel the cripple. But this reasoning is not in the fashion to choose me a husband. O me, the word 'choose!' I may neither choose whom I would nor refuse whom I dislike; so is the will of a living daughter curbed by the will of a dead father. Is it not hard, Nerissa, that I cannot choose one nor refuse none?", 'I pray thee, over-name them; and as thou namest them, I will describe them; and, according to my description, level at my affection.', "Ay, that's a colt indeed, for he doth nothing but talk of his horse; and he makes it a great appropriation to his own good parts, that he can shoe him himself. I am much afeard my lady his mother played false with a smith.", "He doth nothing but frown, as who should say 'If you will not have me, choose:' he hears merry tales and smiles not: I fear he will prove the weeping philosopher when he grows old, being so full of unmannerly sadness in his youth. I had rather be married to a death's-head with a bone in his mouth than to either of these. God defend me from these two!", "God made him, and therefore let him pass for a man. In truth, I know it is a sin to be a mocker: but, he! why, he hath a horse better than the Neapolitan's, a better bad habit of frowning than the Count Palatine; he is every man in no man; if a throstle sing, he falls straight a capering: he will fence with his own shadow: if I should marry him, I should marry twenty husbands. If he would despise me I would forgive him, for if he love me to madness, I shall never requite him.", "You know I say nothing to him, for he understands not me, nor I him: he hath neither Latin, French, nor Italian, and you will come into the court and swear that I have a poor pennyworth in the English. He is a proper man's picture, but, alas, who can converse with a dumb-show? How oddly he is suited! I think he bought his doublet in Italy, his round hose in France, his bonnet in Germany and his behavior every where.", 'That he hath a neighbourly charity in him, for he borrowed a box of the ear of the Englishman and swore he would pay him again when he was able: I think the Frenchman became his surety and sealed under for another.', 'Very vilely in the morning, when he is sober, and most vilely in the afternoon, when he is drunk: when he is best, he is a little worse than a man, and when he is worst, he is little better than a beast: and the worst fall that ever fell, I hope I shall make shift to go without him.', "Therefore, for fear of the worst, I pray thee, set a deep glass of rhenish wine on the contrary casket, for if the devil be within and that temptation without, I know he will choose it. I will do any thing, Nerissa, ere I'll be married to a sponge.", "If I live to be as old as Sibylla, I will die as chaste as Diana, unless I be obtained by the manner of my father's will. I am glad this parcel of wooers are so reasonable, for there is not one among them but I dote on his very absence, and I pray God grant them a fair departure.", 'Yes, yes, it was Bassanio; as I think, he was so called.', 'I remember him well, and I remember him worthy of thy praise. How now! what news?', 'If I could bid the fifth welcome with so good a heart as I can bid the other four farewell, I should be glad of his approach: if he have the condition of a saint and the complexion of a devil, I had rather he should shrive me than wive me. Come, Nerissa. Sirrah, go before. Whiles we shut the gates upon one wooer, another knocks at the door.', "In terms of choice I am not solely led By nice direction of a maiden's eyes; Besides, the lottery of my destiny Bars me the right of voluntary choosing: But if my father had not scanted me And hedged me by his wit, to yield myself His wife who wins me by that means I told you, Yourself, renowned prince, then stood as fair As any comer I have look'd on yet For my affection.", 'You must take your chance, And either not attempt to choose at all Or swear before you choose, if you choose wrong Never to speak to lady afterward In way of marriage: therefore be advised.', 'First, forward to the temple: after dinner Your hazard shall be made.', 'Go draw aside the curtains and discover The several caskets to this noble prince. Now make your choice.', 'The one of them contains my picture, prince: If you choose that, then I am yours withal.', 'There, take it, prince; and if my form lie there, Then I am yours.', 'A gentle riddance. Draw the curtains, go. Let all of his complexion choose me so.', "Behold, there stand the caskets, noble prince: If you choose that wherein I am contain'd, Straight shall our nuptial rites be solemnized: But if you fail, without more speech, my lord, You must be gone from hence immediately.", 'To these injunctions every one doth swear That comes to hazard for my worthless self.', 'Too long a pause for that which you find there.', 'To offend, and judge, are distinct offices And of opposed natures.', 'Thus hath the candle singed the moth. O, these deliberate fools! when they do choose, They have the wisdom by their wit to lose.', 'Come, draw the curtain, Nerissa.', 'Here: what would my lord?', "No more, I pray thee: I am half afeard Thou wilt say anon he is some kin to thee, Thou spend'st such high-day wit in praising him. Come, come, Nerissa; for I long to see Quick Cupid's post that comes so mannerly.", "I pray you, tarry: pause a day or two Before you hazard; for, in choosing wrong, I lose your company: therefore forbear awhile. There's something tells me, but it is not love, I would not lose you; and you know yourself, Hate counsels not in such a quality. But lest you should not understand me well,-- And yet a maiden hath no tongue but thought,-- I would detain you here some month or two Before you venture for me. I could teach you How to choose right, but I am then forsworn; So will I never be: so may you miss me; But if you do, you'll make me wish a sin, That I had been forsworn. Beshrew your eyes, They have o'erlook'd me and divided me; One half of me is yours, the other half yours, Mine own, I would say; but if mine, then yours, And so all yours. O, these naughty times Put bars between the owners and their rights! And so, though yours, not yours. Prove it so, Let fortune go to hell for it, not I. I speak too long; but 'tis to peize the time, To eke it and to draw it out in length, To stay you from election.", 'Upon the rack, Bassanio! then confess What treason there is mingled with your love.', 'Ay, but I fear you speak upon the rack, Where men enforced do speak anything.', 'Well then, confess and live.', "Away, then! I am lock'd in one of them: If you do love me, you will find me out. Nerissa and the rest, stand all aloof. Let music sound while he doth make his choice; Then, if he lose, he makes a swan-like end, Fading in music: that the comparison May stand more proper, my eye shall be the stream And watery death-bed for him. He may win; And what is music then? Then music is Even as the flourish when true subjects bow To a new-crowned monarch: such it is As are those dulcet sounds in break of day That creep into the dreaming bridegroom's ear, And summon him to marriage. Now he goes, With no less presence, but with much more love, Than young Alcides, when he did redeem The virgin tribute paid by howling Troy To the sea-monster: I stand for sacrifice The rest aloof are the Dardanian wives, With bleared visages, come forth to view The issue of the exploit. Go, Hercules! Live thou, I live: with much, much more dismay I view the fight than thou that makest the fray. SONG. Tell me where is fancy bred, Or in the heart, or in the head? How begot, how nourished? Reply, reply. It is engender'd in the eyes, With gazing fed; and fancy dies In the cradle where it lies. Let us all ring fancy's knell I'll begin it,--Ding, dong, bell.", '[Aside] How all the other passions fleet to air, As doubtful thoughts, and rash-embraced despair, And shuddering fear, and green-eyed jealousy! O love, Be moderate; allay thy ecstasy, In measure rein thy joy; scant this excess. I feel too much thy blessing: make it less, For fear I surfeit.', "You see me, Lord Bassanio, where I stand, Such as I am: though for myself alone I would not be ambitious in my wish, To wish myself much better; yet, for you I would be trebled twenty times myself; A thousand times more fair, ten thousand times more rich; That only to stand high in your account, I might in virtue, beauties, livings, friends, Exceed account; but the full sum of me Is sum of something, which, to term in gross, Is an unlesson'd girl, unschool'd, unpractised; Happy in this, she is not yet so old But she may learn; happier than this, She is not bred so dull but she can learn; Happiest of all is that her gentle spirit Commits itself to yours to be directed, As from her lord, her governor, her king. Myself and what is mine to you and yours Is now converted: but now I was the lord Of this fair mansion, master of my servants, Queen o'er myself: and even now, but now, This house, these servants and this same myself Are yours, my lord: I give them with this ring; Which when you part from, lose, or give away, Let it presage the ruin of your love And be my vantage to exclaim on you.", 'Is this true, Nerissa?', 'So do I, my lord: They are entirely welcome.', "There are some shrewd contents in yon same paper, That steals the colour from Bassanio's cheek: Some dear friend dead; else nothing in the world Could turn so much the constitution Of any constant man. What, worse and worse! With leave, Bassanio: I am half yourself, And I must freely have the half of anything That this same paper brings you.", 'Is it your dear friend that is thus in trouble?', 'What sum owes he the Jew?', "What, no more? Pay him six thousand, and deface the bond; Double six thousand, and then treble that, Before a friend of this description Shall lose a hair through Bassanio's fault. First go with me to church and call me wife, And then away to Venice to your friend; For never shall you lie by Portia's side With an unquiet soul. You shall have gold To pay the petty debt twenty times over: When it is paid, bring your true friend along. My maid Nerissa and myself meantime Will live as maids and widows. Come, away! For you shall hence upon your wedding-day: Bid your friends welcome, show a merry cheer: Since you are dear bought, I will love you dear. But let me hear the letter of your friend.", 'O love, dispatch all business, and be gone!', "I never did repent for doing good, Nor shall not now: for in companions That do converse and waste the time together, Whose souls do bear an equal yoke Of love, There must be needs a like proportion Of lineaments, of manners and of spirit; Which makes me think that this Antonio, Being the bosom lover of my lord, Must needs be like my lord. If it be so, How little is the cost I have bestow'd In purchasing the semblance of my soul From out the state of hellish misery! This comes too near the praising of myself; Therefore no more of it: hear other things. Lorenzo, I commit into your hands The husbandry and manage of my house Until my lord's return: for mine own part, I have toward heaven breathed a secret vow To live in prayer and contemplation, Only attended by Nerissa here, Until her husband and my lord's return: There is a monastery two miles off; And there will we abide. I do desire you Not to deny this imposition; The which my love and some necessity Now lays upon you.", 'My people do already know my mind, And will acknowledge you and Jessica In place of Lord Bassanio and myself. And so farewell, till we shall meet again.', "I thank you for your wish, and am well pleased To wish it back on you: fare you well Jessica. Now, Balthasar, As I have ever found thee honest-true, So let me find thee still. Take this same letter, And use thou all the endeavour of a man In speed to Padua: see thou render this Into my cousin's hand, Doctor Bellario; And, look, what notes and garments he doth give thee, Bring them, I pray thee, with imagined speed Unto the tranect, to the common ferry Which trades to Venice. Waste no time in words, But get thee gone: I shall be there before thee.", "Come on, Nerissa; I have work in hand That you yet know not of: we'll see our husbands Before they think of us.", "They shall, Nerissa; but in such a habit, That they shall think we are accomplished With that we lack. I'll hold thee any wager, When we are both accoutred like young men, I'll prove the prettier fellow of the two, And wear my dagger with the braver grace, And speak between the change of man and boy With a reed voice, and turn two mincing steps Into a manly stride, and speak of frays Like a fine bragging youth, and tell quaint lies, How honourable ladies sought my love, Which I denying, they fell sick and died; I could not do withal; then I'll repent, And wish for all that, that I had not killed them; And twenty of these puny lies I'll tell, That men shall swear I have discontinued school Above a twelvemonth. I have within my mind A thousand raw tricks of these bragging Jacks, Which I will practise.", "Fie, what a question's that, If thou wert near a lewd interpreter! But come, I'll tell thee all my whole device When I am in my coach, which stays for us At the park gate; and therefore haste away, For we must measure twenty miles to-day.", 'I did, my lord.', 'I am informed thoroughly of the cause. Which is the merchant here, and which the Jew?', 'Is your name Shylock?', 'Of a strange nature is the suit you follow; Yet in such rule that the Venetian law Cannot impugn you as you do proceed. You stand within his danger, do you not?', 'Do you confess the bond?', 'Then must the Jew be merciful.', "The quality of mercy is not strain'd, It droppeth as the gentle rain from heaven Upon the place beneath: it is twice blest; It blesseth him that gives and him that takes: 'Tis mightiest in the mightiest: it becomes The throned monarch better than his crown; His sceptre shows the force of temporal power, The attribute to awe and majesty, Wherein doth sit the dread and fear of kings; But mercy is above this sceptred sway; It is enthroned in the hearts of kings, It is an attribute to God himself; And earthly power doth then show likest God's When mercy seasons justice. Therefore, Jew, Though justice be thy plea, consider this, That, in the course of justice, none of us Should see salvation: we do pray for mercy; And that same prayer doth teach us all to render The deeds of mercy. I have spoke thus much To mitigate the justice of thy plea; Which if thou follow, this strict court of Venice Must needs give sentence 'gainst the merchant there.", 'Is he not able to discharge the money?', "It must not be; there is no power in Venice Can alter a decree established: 'Twill be recorded for a precedent, And many an error by the same example Will rush into the state: it cannot be.", 'I pray you, let me look upon the bond.', "Shylock, there's thrice thy money offer'd thee.", "Why, this bond is forfeit; And lawfully by this the Jew may claim A pound of flesh, to be by him cut off Nearest the merchant's heart. Be merciful: Take thrice thy money; bid me tear the bond.", 'Why then, thus it is: You must prepare your bosom for his knife.', 'For the intent and purpose of the law Hath full relation to the penalty, Which here appeareth due upon the bond.', 'Therefore lay bare your bosom.', 'It is so. Are there balance here to weigh The flesh?', 'Have by some surgeon, Shylock, on your charge, To stop his wounds, lest he do bleed to death.', "It is not so express'd: but what of that? 'Twere good you do so much for charity.", 'You, merchant, have you any thing to say?', 'Your wife would give you little thanks for that, If she were by, to hear you make the offer.', "A pound of that same merchant's flesh is thine: The court awards it, and the law doth give it.", 'And you must cut this flesh from off his breast: The law allows it, and the court awards it.', "Tarry a little; there is something else. This bond doth give thee here no jot of blood; The words expressly are 'a pound of flesh:' Take then thy bond, take thou thy pound of flesh; But, in the cutting it, if thou dost shed One drop of Christian blood, thy lands and goods Are, by the laws of Venice, confiscate Unto the state of Venice.", 'Thyself shalt see the act: For, as thou urgest justice, be assured Thou shalt have justice, more than thou desirest.', 'Soft! The Jew shall have all justice; soft! no haste: He shall have nothing but the penalty.', "Therefore prepare thee to cut off the flesh. Shed thou no blood, nor cut thou less nor more But just a pound of flesh: if thou cut'st more Or less than a just pound, be it but so much As makes it light or heavy in the substance, Or the division of the twentieth part Of one poor scruple, nay, if the scale do turn But in the estimation of a hair, Thou diest and all thy goods are confiscate.", 'Why doth the Jew pause? take thy forfeiture.', 'He hath refused it in the open court: He shall have merely justice and his bond.', 'Thou shalt have nothing but the forfeiture, To be so taken at thy peril, Jew.', "Tarry, Jew: The law hath yet another hold on you. It is enacted in the laws of Venice, If it be proved against an alien That by direct or indirect attempts He seek the life of any citizen, The party 'gainst the which he doth contrive Shall seize one half his goods; the other half Comes to the privy coffer of the state; And the offender's life lies in the mercy Of the duke only, 'gainst all other voice. In which predicament, I say, thou stand'st; For it appears, by manifest proceeding, That indirectly and directly too Thou hast contrived against the very life Of the defendant; and thou hast incurr'd The danger formerly by me rehearsed. Down therefore and beg mercy of the duke.", 'Ay, for the state, not for Antonio.', 'What mercy can you render him, Antonio?', 'Art thou contented, Jew? what dost thou say?', 'Clerk, draw a deed of gift.', 'I humbly do desire your grace of pardon: I must away this night toward Padua, And it is meet I presently set forth.', 'He is well paid that is well satisfied; And I, delivering you, am satisfied And therein do account myself well paid: My mind was never yet more mercenary. I pray you, know me when we meet again: I wish you well, and so I take my leave.', "You press me far, and therefore I will yield. Give me your gloves, I'll wear them for your sake; And, for your love, I'll take this ring from you: Do not draw back your hand; I'll take no more; And you in love shall not deny me this.", 'I will have nothing else but only this; And now methinks I have a mind to it.', "I see, sir, you are liberal in offers You taught me first to beg; and now methinks You teach me how a beggar should be answer'd.", "That 'scuse serves many men to save their gifts. An if your wife be not a mad-woman, And know how well I have deserved the ring, She would not hold out enemy for ever, For giving it to me. Well, peace be with you!", "Inquire the Jew's house out, give him this deed And let him sign it: we'll away to-night And be a day before our husbands home: This deed will be well welcome to Lorenzo.", "That cannot be: His ring I do accept most thankfully: And so, I pray you, tell him: furthermore, I pray you, show my youth old Shylock's house.", "[Aside to NERISSA] Thou mayst, I warrant. We shall have old swearing That they did give the rings away to men; But we'll outface them, and outswear them too. Away! make haste: thou knowist where I will tarry.", 'That light we see is burning in my hall. How far that little candle throws his beams! So shines a good deed in a naughty world.', 'So doth the greater glory dim the less: A substitute shines brightly as a king Unto the king be by, and then his state Empties itself, as doth an inland brook Into the main of waters. Music! hark!', 'Nothing is good, I see, without respect: Methinks it sounds much sweeter than by day.', "The crow doth sing as sweetly as the lark, When neither is attended, and I think The nightingale, if she should sing by day, When every goose is cackling, would be thought No better a musician than the wren. How many things by season season'd are To their right praise and true perfection! Peace, ho! the moon sleeps with Endymion And would not be awaked.", 'He knows me as the blind man knows the cuckoo, By the bad voice.', "We have been praying for our husbands' healths, Which speed, we hope, the better for our words. Are they return'd?", 'Go in, Nerissa; Give order to my servants that they take No note at all of our being absent hence; Nor you, Lorenzo; Jessica, nor you.', "This night methinks is but the daylight sick; It looks a little paler: 'tis a day, Such as the day is when the sun is hid.", 'Let me give light, but let me not be light; For a light wife doth make a heavy husband, And never be Bassanio so for me: But God sort all! You are welcome home, my lord.', 'You should in all sense be much bound to him. For, as I hear, he was much bound for you.', 'Sir, you are very welcome to our house: It must appear in other ways than words, Therefore I scant this breathing courtesy.', "A quarrel, ho, already! what's the matter?", "You were to blame, I must be plain with you, To part so slightly with your wife's first gift: A thing stuck on with oaths upon your finger And so riveted with faith unto your flesh. I gave my love a ring and made him swear Never to part with it; and here he stands; I dare be sworn for him he would not leave it Nor pluck it from his finger, for the wealth That the world masters. Now, in faith, Gratiano, You give your wife too unkind a cause of grief: An 'twere to me, I should be mad at it.", 'What ring gave you my lord? Not that, I hope, which you received of me.', "Even so void is your false heart of truth. By heaven, I will ne'er come in your bed Until I see the ring.", "If you had known the virtue of the ring, Or half her worthiness that gave the ring, Or your own honour to contain the ring, You would not then have parted with the ring. What man is there so much unreasonable, If you had pleased to have defended it With any terms of zeal, wanted the modesty To urge the thing held as a ceremony? Nerissa teaches me what to believe: I'll die for't but some woman had the ring.", "Let not that doctor e'er come near my house: Since he hath got the jewel that I loved, And that which you did swear to keep for me, I will become as liberal as you; I'll not deny him any thing I have, No, not my body nor my husband's bed: Know him I shall, I am well sure of it: Lie not a night from home; watch me like Argus: If you do not, if I be left alone, Now, by mine honour, which is yet mine own, I'll have that doctor for my bedfellow.", 'Sir, grieve not you; you are welcome notwithstanding.', "Mark you but that! In both my eyes he doubly sees himself; In each eye, one: swear by your double self, And there's an oath of credit.", 'Then you shall be his surety. Give him this And bid him keep it better than the other.', 'I had it of him: pardon me, Bassanio; For, by this ring, the doctor lay with me.', "Speak not so grossly. You are all amazed: Here is a letter; read it at your leisure; It comes from Padua, from Bellario: There you shall find that Portia was the doctor, Nerissa there her clerk: Lorenzo here Shall witness I set forth as soon as you And even but now return'd; I have not yet Enter'd my house. Antonio, you are welcome; And I have better news in store for you Than you expect: unseal this letter soon; There you shall find three of your argosies Are richly come to harbour suddenly: You shall not know by what strange accident I chanced on this letter.", 'How now, Lorenzo! My clerk hath some good comforts too for you.', "It is almost morning, And yet I am sure you are not satisfied Of these events at full. Let us go in; And charge us there upon inter'gatories, And we will answer all things faithfully."] 

的處理和格式化收到的信息的時間:

Your code : 0:00:00.187483 
Above code : 0:00:00.026628 

您可以從上面看到,它比您的代碼快大約7-8 times

注意:這些時間不包括獲取響應所花費的時間,因爲這與代碼無關。

+0

非常感謝! – siowy

相關問題