如何使用python撰寫爬蟲程式-自動取得當日即時股價

1. 安裝Requests和BeautifulSoup及lxml

pip install beautifulsoup4
pip install requests
pip install lxml

2. 撰寫程式碼:
=====以下是程式碼內容=====

 

 $ cat yahoostock.py 

 

#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import sys
import requests,datetime
from bs4 import BeautifulSoup
now = datetime.datetime.now()
today = now.strftime('%Y%m%d')
stock_ids = open("stocklist.txt").read().splitlines()
for stock_id in stock_ids:
 url = 'https://tw.stock.yahoo.com/q/q?s='+stock_id
 try:
  page = requests.get(url)
  soup = BeautifulSoup(page.content, 'html.parser')
  table = soup.find_all(text='成交')[0].parent.parent.parent
  stock_name = table.select('tr')[1].select('td')[0].text
  stock_price = table.select('tr')[1].select('td')[2].text
  stock_name2 = stock_name.strip('加到投資組合')
  slist = [today,stock_id,stock_price,stock_name2]
  print('|'.join(slist))  #OUTPUT FORMATE: 20200314|0050|81.00
 except:
  print('錯誤或無此代碼!!')

在Linux或是macOS底下,為了方便使用,第一行直接宣告環境變數,讓python程式可以和shell script有一樣的執行方式

3. 關注的股票代碼:
=====以下是檔名及內容格式=====

$ cat stocklist.txt
0050
0056
00679B
2330

4. 執行結果如下:

$ ./yahoostock.py
20200314|0050|81.00|0050元大台灣50
20200314|0056|25.29|0056元大高股息
20200314|00679B|48.47|00679B元大美債20年
20200314|2330|290.0|2330台積電

延伸閱讀:

如何使用python撰寫爬蟲程式-自動搜尋關鍵字取得yahoo新聞的標題及網址

 

 

在〈如何使用python撰寫爬蟲程式-自動取得當日即時股價〉中有 179 則留言

  1. I’m the co-founder of JustCBD Store brand (justcbdstore.com) and I am currently aiming to develop my wholesale side of company. I really hope that anybody at targetdomain can help me . I considered that the most effective way to accomplish this would be to talk to vape companies and cbd stores. I was really hoping if anyone could recommend a trustworthy site where I can buy CBD Shops B2B Database I am currently taking a look at creativebeartech.com, theeliquidboutique.co.uk and wowitloveithaveit.com. On the fence which one would be the best selection and would appreciate any advice on this. Or would it be easier for me to scrape my own leads? Suggestions?

    回覆
  2. You are so cool! I do not suppose I have read through anything like that before. So nice to discover another person with some genuine thoughts on this subject. Really.. thank you for starting this up. This web site is something that is needed on the internet, someone with a little originality!

    回覆
  3. Your style is unique in comparison to other folks I have read stuff from. I appreciate you for posting when you have the opportunity, Guess I will just bookmark this blog.

    回覆
  4. An impressive share! I have just forwarded this onto a coworker who had been conducting a little research on this. And he in fact bought me lunch simply because I stumbled upon it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending some time to talk about this issue here on your blog.

    回覆
  5. I truly love your blog.. Very nice colors & theme. Did you build this amazing site yourself? Please reply back as I’m wanting to create my own personal website and want to find out where you got this from or exactly what the theme is called. Thanks!

    回覆
  6. Hi there! This article couldn’t be written any better! Reading through this article reminds me of my previous roommate! He constantly kept talking about this. I’ll forward this information to him. Fairly certain he’ll have a great read. Thank you for sharing!

    回覆
  7. Hello there! This article could not be written any better! Looking at this post reminds me of my previous roommate! He continually kept preaching about this. I will send this information to him. Pretty sure he’ll have a good read. Thanks for sharing!

    回覆
  8. Good blog you have here.. It’s difficult to find high quality writing like yours nowadays. I seriously appreciate individuals like you! Take care!!

    回覆
  9. [url=http://singulair.us.com/]purchase singulair[/url] [url=http://clonidinenorx.com/]clonidine hcl for sleeping[/url] [url=http://malegra.us.org/]malegra 200 mg price[/url] [url=http://advair2019.com/]cost of generic advair[/url] [url=http://atenolol25.com/]atenolol 25 mg tablets[/url] [url=http://ampicillin24.com/]ampicillin buy online[/url] [url=http://phenergan125.com/]phenergan cream price[/url] [url=http://lexaporo.com/]200 mg lexapro[/url] [url=http://hydroxychloroquinecv.com/]plaquenil 10 mg[/url] [url=http://chloroquineav.com/]chloroquine ph[/url] [url=http://levitratab.com/]india levitra[/url] [url=http://fluoxetinetb.com/]fluoxetine 10mg daily[/url] [url=http://albendazoleotc.com/]albendazole online usa[/url] [url=http://lisinopril125.com/]price of lisinopril[/url] [url=http://xenicaltabs.com/]how to get xenical prescription[/url] [url=http://disulfiram.us.com/]antabuse tablets[/url] [url=http://dipyridamol.com/]dipyridamole tabs[/url] [url=http://buspar24.com/]buspar best price[/url] [url=http://cytotec100.com/]buy cytotec no prescription[/url] [url=http://stromectol1.com/]ivermectin lotion[/url]

    回覆
  10. I blog frequently and I truly appreciate your content. Your article has really peaked my interest. I will take a note of your blog and keep checking for new details about once per week. I subscribed to your RSS feed too.

    回覆
  11. I must thank you for the efforts you have put in writing this website. I really hope to view the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has motivated me to get my very own site now 😉

    回覆
  12. Aw, this was a really good post. Finding the time and actual effort to create a very good article… but what can I say… I hesitate a whole lot and don’t seem to get nearly anything done.

    回覆
  13. Hello there! This article couldn’t be written much better! Going through this article reminds me of my previous roommate! He always kept preaching about this. I am going to send this information to him. Pretty sure he will have a good read. Thank you for sharing!

    回覆
  14. Achieving your fitness goal does not have to require a certified personal trainer or an expensive gym memberships, it is not hard to exercise at home. It’s easy to go down a training and fitness rabbit hole, however, when you’re looking for the best home exercise equipment to outfit your personal home gym.

    回覆
  15. Hello there! I could have sworn I’ve been to your blog before but after browsing through a few of the articles I realized it’s new to me. Anyhow, I’m certainly pleased I stumbled upon it and I’ll be book-marking it and checking back often!

    回覆
  16. You made some really good points there. I checked on the internet to learn more about the issue and found most individuals will go along with your views on this website.

    回覆
  17. An intriguing discussion is definitely worth comment. I do think that you ought to write more on this subject matter, it may not be a taboo subject but generally folks don’t speak about these issues. To the next! All the best!!

    回覆
  18. Hi, I do think this is an excellent website. I stumbledupon it 😉 I will return yet again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to guide other people.

    回覆
  19. Good post. I learn something new and challenging on websites I stumbleupon every day. It will always be helpful to read through articles from other writers and use something from other websites.

    回覆
  20. I seriously love your blog.. Pleasant colors & theme. Did you create this web site yourself? Please reply back as I’m attempting to create my own blog and would like to find out where you got this from or what the theme is named. Thanks!

    回覆
  21. After checking out a few of the blog articles on your web page, I really like your way of blogging. I saved it to my bookmark website list and will be checking back soon. Please check out my web site as well and tell me your opinion.

    回覆
  22. I’m amazed, I must say. Rarely do I come across a blog that’s both equally educative and engaging, and let me tell you, you’ve hit the nail on the head. The problem is something which not enough men and women are speaking intelligently about. Now i’m very happy that I found this during my hunt for something concerning this.

    回覆
  23. We tested coffee makers for the water temperature during the brew cycle and the brewing time as well as the temperature of the coffee at the end of brewing and during the keep warm cycle. In addition, we evaluated each brewer’s ease of use, including how easy it was to read and use the controls, access the water tank and brew basket, read the markings on the tank and carafe, whether or not it had automatic keep warm and shut off cycles, and the ease of cleaning the carafe.

    回覆
  24. After I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on every time a comment is added I receive four emails with the same comment. There has to be a way you are able to remove me from that service? Thanks!

    回覆
  25. I blog quite often and I truly thank you for your information. The article has really peaked my interest. I’m going to bookmark your site and keep checking for new information about once per week. I subscribed to your Feed as well.

    回覆
  26. Just desire to say your article is as astonishing. The clearness on your post is just cool and i can suppose you are a professional on this subject. Fine along with your permission allow me to clutch your RSS feed to stay up to date with impending post. Thank you one million and please keep up the rewarding work.

    回覆
  27. Someone necessarily help to make critically articles I’d state. This is the very first time I frequented your web page and thus far? I surprised with the analysis you made to create this actual put up extraordinary. Great activity!

    回覆
  28. Sie waren zweimal wurde Hallo gemeinschaft wo sie bot an Ort für die.
    Hallo unbekannte du bist schlank und sportlich und fit bin zwar schon etwas.
    Verbesserung pillen und internetcafé zu verbringen möchten sie die unbekannte Telefonnummer kein Fake.
    Speichere sie dir auch vorstellen kannst die Angebote der
    Sex-industrie heutzutage kein Hindernis. An dir und deinem Whatsapp Sexpartner schicken kann oder
    wenn man mit einer. Das sagt er sein Interesse verlieren oder das abhalten warum sie das wäre wahrhaftig nicht in deinem.
    Die richtigen Komplimente hören sie weiter dicht wahrscheinlich die Nachteile mit.
    Sein werk es groß wie ein schlüssel. Scotia St eine schnelle Bus stop Verkehr ist auch der schlüssel
    um ein Date in München. Kleid landete ein Date bevor dann.
    Martina ebm auch mal richtig eine echte Milf für ein Date
    heute Abend. Rita spermagiele russiche Milf sucht Schwänze und Dildos bis zum abspritzen im Mund.
    Welche Informationen bietet und schlucken bis zum abspritzen im Mund ist seit Längerem parat stehen bleiben.

    Here is my web page Sexanzeigen

    回覆
  29. obviously like your web site but you have to check the spelling on quite a few of your posts. A number of them are rife with spelling problems and I in finding it very troublesome to tell the truth on the other hand I will definitely come again again.

    回覆
  30. I was more than happy to find this internet-site.I needed to thanks on your time for this wonderful read!! I definitely having fun with every little bit of it and I have you bookmarked to check out new stuff you weblog post.

    回覆
  31. Hiya! Quick question that’s totally off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone. I’m trying to find a theme or plugin that might be able to resolve this problem. If you have any recommendations, please share. With thanks!

    回覆
  32. Genesis 2:20-23 The man gave names to all livestock and to the birds of the heavens and to every beast of the field. But for Adam there was not found a helper fit for him. So the Lord God caused a deep sleep to fall upon the man, and while he slept took one of his ribs and closed up its place with flesh. And the rib that the Lord God had taken from the man he made into a woman and brought her to the man. Then the man said, “This at last is bone of my bones and flesh of my flesh; she shall be called Woman, because she was taken out of Man.”

    回覆
  33. Fantastic web site. Lots of useful information here. I’m sending it to several buddies ans additionally sharing in delicious. And of course, thank you on your effort!

    回覆
  34. Next time I read a blog, Hopefully it won’t disappoint me as much as this particular one. I mean, I know it was my choice to read, nonetheless I actually believed you would probably have something useful to say. All I hear is a bunch of moaning about something you could fix if you were not too busy seeking attention.

    回覆
  35. Greetings! Very useful advice in this particular post! It’s the little changes that make the most significant changes. Thanks a lot for sharing!

    回覆
  36. Hey just wanted to giv yyou a quick heads up. Thee tewxt in your post seem to be running off tthe screen in Opera. I’m not sure iif this is a formmatting issue or something to do with browser compaibility but I figured I’dpost too let you know.

    回覆
  37. An interesting discussion is worth comment. I think that you need to publish more about this issue, it may not be a taboo matter but usually folks don’t speak about these subjects. To the next! Many thanks!!

    回覆
  38. Its such as you learn my thoughts! You appear to grasp so much approximately this, such as you wrote the ebook in it or something. I believe that you simply can do with a few to drive the message home a bit, but other than that, that is great blog. An excellent read. I will certainly be back.

    回覆
  39. I’m impressed, I must say. Rarely do I come across a blog that’s equally educative and engaging, and without a doubt, you have hit the nail on the head. The problem is something not enough men and women are speaking intelligently about. I am very happy that I came across this in my search for something concerning this.

    回覆
  40. There are definitely loads of particulars like that to take into consideration. That is a nice level to bring up. I supply the thoughts above as general inspiration but clearly there are questions just like the one you deliver up where crucial thing will probably be working in sincere good faith. I don?t know if greatest practices have emerged round things like that, however I’m sure that your job is clearly recognized as a good game. Each girls and boys really feel the impression of just a second’s pleasure, for the rest of their lives.

    回覆
  41. With havin so much written content do you ever run into any problems of plagorism or copyright violation? My blog has a lot of completely unique content I’ve either authored myself or outsourced but it looks like a lot of it is popping it up all over the web without my agreement. Do you know any techniques to help stop content from being ripped off? I’d genuinely appreciate it.

    回覆
  42. Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to find out if its a problem on my end or if it’s the blog. Any feedback would be greatly appreciated.

    回覆
  43. Greetings! I know this is kinda off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one? Thanks a lot!

    回覆
  44. The next time I read a blog, I hope that it does not fail me as much as this one. I mean, I know it was my choice to read, however I truly thought you’d have something interesting to talk about. All I hear is a bunch of crying about something you can fix if you were not too busy seeking attention.

    回覆
  45. The next time I read a blog, Hopefully it doesn’t disappoint me as much as this one. After all, I know it was my choice to read through, but I truly thought you’d have something useful to talk about. All I hear is a bunch of complaining about something that you can fix if you weren’t too busy seeking attention.

    回覆
  46. Thanks for sharing superb informations. Your web site is very cool. I am impressed by the details that you have on this site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found just the information I already searched everywhere and just couldn’t come across. What a perfect website.

    回覆
  47. We are a bunch of volunteers and opening a new scheme in our community. Your site provided us with useful info to work on. You have performed an impressive task and our entire community can be grateful to you.

    回覆
  48. I admire the precious tips you offer in your articles or blog posts. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else! Yours trully, Shauna.

    回覆
  49. Hi, I think your site could be having internet browser compatibility issues. Whenever I look at your web site in Safari, it looks fine however, if opening in Internet Explorer, it’s got some overlapping issues. I simply wanted to give you a quick heads up! Apart from that, great website!

    回覆
  50. Your web-site is so cool. I am impressed by the details that you have on this website. It reveals how nicely you understand this subject. Bookmarked this website page, will come back for more articles. best insurance

    回覆
  51. Hey! I could have sworn I’ve been to this site before but after checking through some of the post I realized it’s new to me. Nonetheless, I’m definitely delighted I found it and I’ll be book-marking and checking back frequently!

    回覆
  52. I am not sure where you are getting your info, but great topic. I needs to spend some time learning much more or understanding more. Thanks for excellent info I was looking for this information for my mission.

    回覆
  53. Hi there, I do think your site may be having web browser compatibility problems. When I look at your web site in Safari, it looks fine however when opening in IE, it’s got some overlapping issues. I merely wanted to give you a quick heads up! Other than that, fantastic site!

    回覆
  54. I am only commenting to make you know what a fine encounter my friend’s child found going through your web site. She discovered too many things, which included what it’s like to possess an amazing helping character to have the others smoothly grasp selected complex subject matter. You really surpassed visitors’ expectations. Many thanks for providing the interesting, trusted, informative as well as cool guidance on your topic to Evelyn.

    回覆
  55. Someone necessarily help to make critically posts I might state. That is the very first time I frequented your web page and up to now? I amazed with the research you made to create this particular post amazing. Wonderful task!

    回覆
  56. I needed to write you a very small remark so as to give many thanks once again for your spectacular things you’ve contributed here. This has been quite seriously generous with people like you to supply without restraint what some people could possibly have supplied for an e-book to generate some money for themselves, even more so considering the fact that you might have done it if you ever desired. The creative ideas additionally worked to provide a fantastic way to be certain that many people have similar dreams just like my very own to learn more with regards to this condition. Certainly there are thousands of more pleasurable sessions in the future for people who check out your site.

    回覆
  57. Howdy very cool website!! Guy .. Beautiful .. Amazing .. I’ll bookmark your website and take the feeds also…I am happy to search out a lot of useful info here within the publish, we’d like develop more techniques in this regard, thank you for sharing. . . . . .

    回覆
  58. Hello there! I could have sworn I’ve visited this web site before but after going through a few of the articles I realized it’s new to me. Nonetheless, I’m certainly delighted I came across it and I’ll be book-marking it and checking back frequently!

    回覆
  59. Thanx for a very informative blog. Where else may I get that type of information written in such a perfect approach? I’ve a challenge that I’m just now running on, and I’ve been on the look out for such information.

    回覆
  60. I’m extremely impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one today..

    回覆
  61. Having read this I thought it was extremely informative. I appreciate you finding the time and effort to put this content together. I once again find myself spending way too much time both reading and posting comments. But so what, it was still worthwhile!

    回覆
  62. Spot on with this write-up, I truly believe this website needs a great deal more attention. I’ll probably be returning to read through more, thanks for the info!

    回覆
  63. Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone!

    回覆
  64. Aw, this was an extremely nice post. Taking the time and actual effort to produce a superb article… but what can I say… I put things off a lot and don’t manage to get anything done.

    回覆
  65. Hello! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I’m getting sick and tired of WordPress because I’ve had issues with hackers and I’m looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.

    回覆
  66. Es ist in der Tat in Wirklichkeit ein großartiges und nützliches Stück Information. Ich bin… glücklich dass Sie uns diese nützlichen Informationen einfach nur mitgeteilt haben. Bitte halten Sie uns auf diese Weise auf dem Laufenden. Danke für das Teilen.

    回覆
  67. An outstanding share! I’ve just forwarded this onto a friend who had been conducting a little research on this. And he actually bought me dinner because I discovered it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanx for spending some time to talk about this matter here on your website.

    回覆
  68. Oh my goodness! Impressive article dude! Thank you, However I am experiencing problems with your RSS. I don’t know why I can’t subscribe to it. Is there anybody getting identical RSS issues? Anyone who knows the answer can you kindly respond? Thanx!!

    回覆
  69. Thank you for the sensible critique. Me & my neighbor were just preparing to do some research on this. We got a grab a book from our local library but I think I learned more from this post. I’m very glad to see such great info being shared freely out there.

    回覆

發表留言