{"id":2233,"date":"2022-03-02T23:14:19","date_gmt":"2022-03-02T23:14:19","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=2233"},"modified":"2022-03-02T23:17:15","modified_gmt":"2022-03-02T23:17:15","slug":"web-scraping-webpages-that-require-login-info","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/web-scraping-webpages-that-require-login-info\/","title":{"rendered":"Web Scraping Webpages that Require Login info"},"content":{"rendered":"<p>Someone recently asked me how to go about web scraping a webpage that requires you to login. The issue with these types of situations is that the URL you are scraping wont allow you to access the data, even if you are signed into the website. The solution is that within R you actually need to set up your login info prior to scraping from the desired URL.<\/p>\n<p>Let&#8217;s take a simple example. Here is the player Approximate Value page from Pro-Football-Reference:<\/p>\n<p><span style=\"color: #0000ff;\"><strong><a style=\"color: #0000ff;\" href=\"https:\/\/stathead.com\/football\/player-season-finder.cgi?request=1&amp;draft_slot_min=1&amp;draft_year_max=2021&amp;order_by=av&amp;season_start=1&amp;draft_pick_in_round=pick_overall&amp;pro_bowls_comp=gt&amp;weight_max=500&amp;order_by_asc=0&amp;seasons_played_comp=gt&amp;conference=any&amp;year_min=2010&amp;all_pros_first_team_comp=gt&amp;phase_type=reg&amp;draft_slot_max=500&amp;match=player_season&amp;year_max=2021&amp;season_end=-1&amp;draft_year_min=1936&amp;age_min=0&amp;age_max=99&amp;comp_id=1&amp;offset=0\">URL<\/a><\/strong><\/span><\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.55.28-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2234\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.55.28-PM-1024x511.png\" alt=\"\" width=\"625\" height=\"312\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.55.28-PM-1024x511.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.55.28-PM-300x150.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.55.28-PM-768x383.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.55.28-PM-624x311.png 624w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>Notice that the first 10 rows are not accessible to us because they are part of a subscription service. Also, if we were to navigate to the URL (as opposed to looking at my small screen shot), you&#8217;ll see only 20 rows of data, when the actual page (once logged in) has 200 rows!<\/p>\n<p>Once I log into the account I can see all of the hidden info:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.58.20-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-2235\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.58.20-PM-682x1024.png\" alt=\"\" width=\"292\" height=\"439\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.58.20-PM-682x1024.png 682w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.58.20-PM-200x300.png 200w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.58.20-PM-768x1154.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.58.20-PM-624x937.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-2.58.20-PM.png 944w\" sizes=\"auto, (max-width: 292px) 100vw, 292px\" \/><\/a><\/p>\n<p>Let&#8217;s start by seeing what happens when I scrape this page, now that I&#8217;m logged in<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.00.42-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2236\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.00.42-PM-1024x303.png\" alt=\"\" width=\"625\" height=\"185\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.00.42-PM-1024x303.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.00.42-PM-300x89.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.00.42-PM-768x228.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.00.42-PM-624x185.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.00.42-PM.png 1370w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><br \/>\n<a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.01.17-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-2237\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.01.17-PM.png\" alt=\"\" width=\"402\" height=\"369\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.01.17-PM.png 814w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.01.17-PM-300x275.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.01.17-PM-768x704.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.01.17-PM-624x572.png 624w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/a><br \/>\nACK!! It didn&#8217;t work. Even though I&#8217;m logged into the website, R doesn&#8217;t know it. It just reads the URL as is and thus only returns 20 rows of data with 10 of the rows being blank, since they are covered up on the website.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Solving the Problem<\/strong><\/span><\/p>\n<p>The way we will solve this problem is to get the URL for the login page and pass it to the <strong>session() <\/strong>function in R:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.04.29-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-2238\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.04.29-PM.png\" alt=\"\" width=\"541\" height=\"58\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.04.29-PM.png 916w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.04.29-PM-300x32.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.04.29-PM-768x82.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.04.29-PM-624x67.png 624w\" sizes=\"auto, (max-width: 541px) 100vw, 541px\" \/><\/a><br \/>\nOnce you&#8217;ve loaded up the login page then you need to get a login form and fill it out with your username and password, like this:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.06.29-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-2239\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.06.29-PM.png\" alt=\"\" width=\"414\" height=\"185\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.06.29-PM.png 734w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.06.29-PM-300x134.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.06.29-PM-624x279.png 624w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><\/a><strong>Technical Note: <\/strong><em>The last step where you are changing the info in field 4 to &#8220;button&#8221; is required, otherwise you will get an error in the next step. I&#8217;m not entirely sure why it is required and after failing a few times and then doing some googling, I&#8217;ve found this to be an easy enough solution.<\/em><\/p>\n<p>Once you have filled out the login form, you simply submit it to the website with the <strong>session_submit()<\/strong> function and then repeat the web scraping process that we did at the beginning, however instead of using the <strong>read_html()<\/strong> function to pass the URL you will use the <strong>session_jump_to()<\/strong> function and provide it with the info about the login page and the URL you are scraping:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.10.55-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2240\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.10.55-PM-1024x309.png\" alt=\"\" width=\"625\" height=\"189\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.10.55-PM-1024x309.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.10.55-PM-300x90.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.10.55-PM-768x232.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.10.55-PM-624x188.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.10.55-PM.png 1346w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><br \/>\nNow, all the data on that page will be available to you:<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.11.11-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-2241\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.11.11-PM.png\" alt=\"\" width=\"504\" height=\"455\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.11.11-PM.png 940w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.11.11-PM-300x271.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.11.11-PM-768x693.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/03\/Screen-Shot-2022-03-02-at-3.11.11-PM-624x563.png 624w\" sizes=\"auto, (max-width: 504px) 100vw, 504px\" \/><\/a><\/p>\n<p>Happy scraping!!<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Someone recently asked me how to go about web scraping a webpage that requires you to login. The issue with these types of situations is that the URL you are scraping wont allow you to access the data, even if you are signed into the website. The solution is that within R you actually need [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-2233","post","type-post","status-publish","format-standard","hentry","category-r-tips-tricks"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/comments?post=2233"}],"version-history":[{"count":3,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2233\/revisions"}],"predecessor-version":[{"id":2244,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2233\/revisions\/2244"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=2233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=2233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=2233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}