{ "cells": [ { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "import json\n", "import feedparser" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "headers = {\n", " 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0'\n", "}" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "ticker = 'APPLE finance News'" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "rssfeedurl = 'https://feeds.finance.yahoo.com/rss/2.0/headline?s=%s®ion=US&lang=en-US'%ticker" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'https://feeds.finance.yahoo.com/rss/2.0/headline?s=APPLE finance News®ion=US&lang=en-US'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rssfeedurl" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "ename": "InvalidURL", "evalue": "URL can't contain control characters. '/rss/2.0/headline?s=APPLE finance News®ion=US&lang=en-US' (found at least ' ')", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mInvalidURL\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[49], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m NewsFeed \u001b[39m=\u001b[39m feedparser\u001b[39m.\u001b[39;49mparse(rssfeedurl)\n", "File \u001b[0;32m~/Library/Python/3.9/lib/python/site-packages/feedparser/api.py:216\u001b[0m, in \u001b[0;36mparse\u001b[0;34m(url_file_stream_or_string, etag, modified, agent, referrer, handlers, request_headers, response_headers, resolve_relative_uris, sanitize_html)\u001b[0m\n\u001b[1;32m 208\u001b[0m result \u001b[39m=\u001b[39m FeedParserDict(\n\u001b[1;32m 209\u001b[0m bozo\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m,\n\u001b[1;32m 210\u001b[0m entries\u001b[39m=\u001b[39m[],\n\u001b[1;32m 211\u001b[0m feed\u001b[39m=\u001b[39mFeedParserDict(),\n\u001b[1;32m 212\u001b[0m headers\u001b[39m=\u001b[39m{},\n\u001b[1;32m 213\u001b[0m )\n\u001b[1;32m 215\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 216\u001b[0m data \u001b[39m=\u001b[39m _open_resource(url_file_stream_or_string, etag, modified, agent, referrer, handlers, request_headers, result)\n\u001b[1;32m 217\u001b[0m \u001b[39mexcept\u001b[39;00m urllib\u001b[39m.\u001b[39merror\u001b[39m.\u001b[39mURLError \u001b[39mas\u001b[39;00m error:\n\u001b[1;32m 218\u001b[0m result\u001b[39m.\u001b[39mupdate({\n\u001b[1;32m 219\u001b[0m \u001b[39m'\u001b[39m\u001b[39mbozo\u001b[39m\u001b[39m'\u001b[39m: \u001b[39mTrue\u001b[39;00m,\n\u001b[1;32m 220\u001b[0m \u001b[39m'\u001b[39m\u001b[39mbozo_exception\u001b[39m\u001b[39m'\u001b[39m: error,\n\u001b[1;32m 221\u001b[0m })\n", "File \u001b[0;32m~/Library/Python/3.9/lib/python/site-packages/feedparser/api.py:115\u001b[0m, in \u001b[0;36m_open_resource\u001b[0;34m(url_file_stream_or_string, etag, modified, agent, referrer, handlers, request_headers, result)\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[39mreturn\u001b[39;00m url_file_stream_or_string\u001b[39m.\u001b[39mread()\n\u001b[1;32m 113\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(url_file_stream_or_string, \u001b[39mstr\u001b[39m) \\\n\u001b[1;32m 114\u001b[0m \u001b[39mand\u001b[39;00m urllib\u001b[39m.\u001b[39mparse\u001b[39m.\u001b[39murlparse(url_file_stream_or_string)[\u001b[39m0\u001b[39m] \u001b[39min\u001b[39;00m (\u001b[39m'\u001b[39m\u001b[39mhttp\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mhttps\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mftp\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mfile\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mfeed\u001b[39m\u001b[39m'\u001b[39m):\n\u001b[0;32m--> 115\u001b[0m \u001b[39mreturn\u001b[39;00m http\u001b[39m.\u001b[39;49mget(url_file_stream_or_string, etag, modified, agent, referrer, handlers, request_headers, result)\n\u001b[1;32m 117\u001b[0m \u001b[39m# try to open with native open function (if url_file_stream_or_string is a filename)\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[39mtry\u001b[39;00m:\n", "File \u001b[0;32m~/Library/Python/3.9/lib/python/site-packages/feedparser/http.py:171\u001b[0m, in \u001b[0;36mget\u001b[0;34m(url, etag, modified, agent, referrer, handlers, request_headers, result)\u001b[0m\n\u001b[1;32m 169\u001b[0m opener \u001b[39m=\u001b[39m urllib\u001b[39m.\u001b[39mrequest\u001b[39m.\u001b[39mbuild_opener(\u001b[39m*\u001b[39m\u001b[39mtuple\u001b[39m(handlers \u001b[39m+\u001b[39m [_FeedURLHandler()]))\n\u001b[1;32m 170\u001b[0m opener\u001b[39m.\u001b[39maddheaders \u001b[39m=\u001b[39m [] \u001b[39m# RMK - must clear so we only send our custom User-Agent\u001b[39;00m\n\u001b[0;32m--> 171\u001b[0m f \u001b[39m=\u001b[39m opener\u001b[39m.\u001b[39;49mopen(request)\n\u001b[1;32m 172\u001b[0m data \u001b[39m=\u001b[39m f\u001b[39m.\u001b[39mread()\n\u001b[1;32m 173\u001b[0m f\u001b[39m.\u001b[39mclose()\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py:517\u001b[0m, in \u001b[0;36mOpenerDirector.open\u001b[0;34m(self, fullurl, data, timeout)\u001b[0m\n\u001b[1;32m 514\u001b[0m req \u001b[39m=\u001b[39m meth(req)\n\u001b[1;32m 516\u001b[0m sys\u001b[39m.\u001b[39maudit(\u001b[39m'\u001b[39m\u001b[39murllib.Request\u001b[39m\u001b[39m'\u001b[39m, req\u001b[39m.\u001b[39mfull_url, req\u001b[39m.\u001b[39mdata, req\u001b[39m.\u001b[39mheaders, req\u001b[39m.\u001b[39mget_method())\n\u001b[0;32m--> 517\u001b[0m response \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_open(req, data)\n\u001b[1;32m 519\u001b[0m \u001b[39m# post-process response\u001b[39;00m\n\u001b[1;32m 520\u001b[0m meth_name \u001b[39m=\u001b[39m protocol\u001b[39m+\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m_response\u001b[39m\u001b[39m\"\u001b[39m\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py:534\u001b[0m, in \u001b[0;36mOpenerDirector._open\u001b[0;34m(self, req, data)\u001b[0m\n\u001b[1;32m 531\u001b[0m \u001b[39mreturn\u001b[39;00m result\n\u001b[1;32m 533\u001b[0m protocol \u001b[39m=\u001b[39m req\u001b[39m.\u001b[39mtype\n\u001b[0;32m--> 534\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_call_chain(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mhandle_open, protocol, protocol \u001b[39m+\u001b[39;49m\n\u001b[1;32m 535\u001b[0m \u001b[39m'\u001b[39;49m\u001b[39m_open\u001b[39;49m\u001b[39m'\u001b[39;49m, req)\n\u001b[1;32m 536\u001b[0m \u001b[39mif\u001b[39;00m result:\n\u001b[1;32m 537\u001b[0m \u001b[39mreturn\u001b[39;00m result\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py:494\u001b[0m, in \u001b[0;36mOpenerDirector._call_chain\u001b[0;34m(self, chain, kind, meth_name, *args)\u001b[0m\n\u001b[1;32m 492\u001b[0m \u001b[39mfor\u001b[39;00m handler \u001b[39min\u001b[39;00m handlers:\n\u001b[1;32m 493\u001b[0m func \u001b[39m=\u001b[39m \u001b[39mgetattr\u001b[39m(handler, meth_name)\n\u001b[0;32m--> 494\u001b[0m result \u001b[39m=\u001b[39m func(\u001b[39m*\u001b[39;49margs)\n\u001b[1;32m 495\u001b[0m \u001b[39mif\u001b[39;00m result \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 496\u001b[0m \u001b[39mreturn\u001b[39;00m result\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py:1389\u001b[0m, in \u001b[0;36mHTTPSHandler.https_open\u001b[0;34m(self, req)\u001b[0m\n\u001b[1;32m 1388\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mhttps_open\u001b[39m(\u001b[39mself\u001b[39m, req):\n\u001b[0;32m-> 1389\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mdo_open(http\u001b[39m.\u001b[39;49mclient\u001b[39m.\u001b[39;49mHTTPSConnection, req,\n\u001b[1;32m 1390\u001b[0m context\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_context, check_hostname\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_check_hostname)\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/urllib/request.py:1346\u001b[0m, in \u001b[0;36mAbstractHTTPHandler.do_open\u001b[0;34m(self, http_class, req, **http_conn_args)\u001b[0m\n\u001b[1;32m 1344\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 1345\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m-> 1346\u001b[0m h\u001b[39m.\u001b[39;49mrequest(req\u001b[39m.\u001b[39;49mget_method(), req\u001b[39m.\u001b[39;49mselector, req\u001b[39m.\u001b[39;49mdata, headers,\n\u001b[1;32m 1347\u001b[0m encode_chunked\u001b[39m=\u001b[39;49mreq\u001b[39m.\u001b[39;49mhas_header(\u001b[39m'\u001b[39;49m\u001b[39mTransfer-encoding\u001b[39;49m\u001b[39m'\u001b[39;49m))\n\u001b[1;32m 1348\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mOSError\u001b[39;00m \u001b[39mas\u001b[39;00m err: \u001b[39m# timeout error\u001b[39;00m\n\u001b[1;32m 1349\u001b[0m \u001b[39mraise\u001b[39;00m URLError(err)\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py:1257\u001b[0m, in \u001b[0;36mHTTPConnection.request\u001b[0;34m(self, method, url, body, headers, encode_chunked)\u001b[0m\n\u001b[1;32m 1254\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mrequest\u001b[39m(\u001b[39mself\u001b[39m, method, url, body\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m, headers\u001b[39m=\u001b[39m{}, \u001b[39m*\u001b[39m,\n\u001b[1;32m 1255\u001b[0m encode_chunked\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m):\n\u001b[1;32m 1256\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Send a complete request to the server.\"\"\"\u001b[39;00m\n\u001b[0;32m-> 1257\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_send_request(method, url, body, headers, encode_chunked)\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py:1268\u001b[0m, in \u001b[0;36mHTTPConnection._send_request\u001b[0;34m(self, method, url, body, headers, encode_chunked)\u001b[0m\n\u001b[1;32m 1265\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39m'\u001b[39m\u001b[39maccept-encoding\u001b[39m\u001b[39m'\u001b[39m \u001b[39min\u001b[39;00m header_names:\n\u001b[1;32m 1266\u001b[0m skips[\u001b[39m'\u001b[39m\u001b[39mskip_accept_encoding\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m \u001b[39m1\u001b[39m\n\u001b[0;32m-> 1268\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mputrequest(method, url, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mskips)\n\u001b[1;32m 1270\u001b[0m \u001b[39m# chunked encoding will happen if HTTP/1.1 is used and either\u001b[39;00m\n\u001b[1;32m 1271\u001b[0m \u001b[39m# the caller passes encode_chunked=True or the following\u001b[39;00m\n\u001b[1;32m 1272\u001b[0m \u001b[39m# conditions hold:\u001b[39;00m\n\u001b[1;32m 1273\u001b[0m \u001b[39m# 1. content-length has not been explicitly set\u001b[39;00m\n\u001b[1;32m 1274\u001b[0m \u001b[39m# 2. the body is a file or iterable, but not a str or bytes-like\u001b[39;00m\n\u001b[1;32m 1275\u001b[0m \u001b[39m# 3. Transfer-Encoding has NOT been explicitly set by the caller\u001b[39;00m\n\u001b[1;32m 1277\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39m'\u001b[39m\u001b[39mcontent-length\u001b[39m\u001b[39m'\u001b[39m \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m header_names:\n\u001b[1;32m 1278\u001b[0m \u001b[39m# only chunk body if not explicitly set for backwards\u001b[39;00m\n\u001b[1;32m 1279\u001b[0m \u001b[39m# compatibility, assuming the client code is already handling the\u001b[39;00m\n\u001b[1;32m 1280\u001b[0m \u001b[39m# chunking\u001b[39;00m\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py:1102\u001b[0m, in \u001b[0;36mHTTPConnection.putrequest\u001b[0;34m(self, method, url, skip_host, skip_accept_encoding)\u001b[0m\n\u001b[1;32m 1099\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_method \u001b[39m=\u001b[39m method\n\u001b[1;32m 1101\u001b[0m url \u001b[39m=\u001b[39m url \u001b[39mor\u001b[39;00m \u001b[39m'\u001b[39m\u001b[39m/\u001b[39m\u001b[39m'\u001b[39m\n\u001b[0;32m-> 1102\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_validate_path(url)\n\u001b[1;32m 1104\u001b[0m request \u001b[39m=\u001b[39m \u001b[39m'\u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m \u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m \u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m'\u001b[39m \u001b[39m%\u001b[39m (method, url, \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_http_vsn_str)\n\u001b[1;32m 1106\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_output(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_encode_request(request))\n", "File \u001b[0;32m/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/http/client.py:1202\u001b[0m, in \u001b[0;36mHTTPConnection._validate_path\u001b[0;34m(self, url)\u001b[0m\n\u001b[1;32m 1200\u001b[0m match \u001b[39m=\u001b[39m _contains_disallowed_url_pchar_re\u001b[39m.\u001b[39msearch(url)\n\u001b[1;32m 1201\u001b[0m \u001b[39mif\u001b[39;00m match:\n\u001b[0;32m-> 1202\u001b[0m \u001b[39mraise\u001b[39;00m InvalidURL(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mURL can\u001b[39m\u001b[39m'\u001b[39m\u001b[39mt contain control characters. \u001b[39m\u001b[39m{\u001b[39;00murl\u001b[39m!r}\u001b[39;00m\u001b[39m \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m 1203\u001b[0m \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m(found at least \u001b[39m\u001b[39m{\u001b[39;00mmatch\u001b[39m.\u001b[39mgroup()\u001b[39m!r}\u001b[39;00m\u001b[39m)\u001b[39m\u001b[39m\"\u001b[39m)\n", "\u001b[0;31mInvalidURL\u001b[0m: URL can't contain control characters. '/rss/2.0/headline?s=APPLE finance News®ion=US&lang=en-US' (found at least ' ')" ] } ], "source": [ "NewsFeed = feedparser.parse(rssfeedurl)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "feedparser.util.FeedParserDict" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(NewsFeed)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['bozo', 'entries', 'feed', 'headers', 'href', 'status', 'encoding', 'version', 'namespaces'])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "NewsFeed.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(NewsFeed.entries)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'summary': 'In this article, we will be taking a look at the 10 stocks ChatGPT said will make me rich in 10 years. To skip our detailed analysis of generative AI and its application in the field of investing, you can go directly to see the 5 Stocks ChatGPT Said Will Make Me Rich in 10 Years. […]',\n", " 'summary_detail': {'type': 'text/html',\n", " 'language': None,\n", " 'base': 'https://feeds.finance.yahoo.com/rss/2.0/headline?s=AAPL®ion=US&lang=en-US',\n", " 'value': 'In this article, we will be taking a look at the 10 stocks ChatGPT said will make me rich in 10 years. To skip our detailed analysis of generative AI and its application in the field of investing, you can go directly to see the 5 Stocks ChatGPT Said Will Make Me Rich in 10 Years. […]'},\n", " 'id': '5ab07694-9da7-320d-bf6b-a51044b80e44',\n", " 'guidislink': False,\n", " 'links': [{'rel': 'alternate',\n", " 'type': 'text/html',\n", " 'href': 'https://finance.yahoo.com/news/10-stocks-chatgpt-said-rich-202351994.html?.tsrc=rss'}],\n", " 'link': 'https://finance.yahoo.com/news/10-stocks-chatgpt-said-rich-202351994.html?.tsrc=rss',\n", " 'published': 'Sun, 20 Aug 2023 20:23:51 +0000',\n", " 'published_parsed': time.struct_time(tm_year=2023, tm_mon=8, tm_mday=20, tm_hour=20, tm_min=23, tm_sec=51, tm_wday=6, tm_yday=232, tm_isdst=0),\n", " 'title': '10 Stocks ChatGPT Said Will Make Me Rich in 10 Years',\n", " 'title_detail': {'type': 'text/plain',\n", " 'language': None,\n", " 'base': 'https://feeds.finance.yahoo.com/rss/2.0/headline?s=AAPL®ion=US&lang=en-US',\n", " 'value': '10 Stocks ChatGPT Said Will Make Me Rich in 10 Years'}}" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "NewsFeed.entries[0]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }