itsnotaboutthecell

Using Power Query M to Scrape The Web

Jul 9th, 2018
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. /*
  2. Created By: Alex Powers
  3. Website: http://www.itsnotaboutthecell.com
  4. LinkedIn: https://www.linkedin.com/in/alexmpowers/
  5.  
  6. */
  7.  
  8. Comments:
  9. The techincal specifications guide is/was a great resource for me - as dry as these things may be I found it to be very informative and really allowed me to take that next step in my own personal development:
  10. URL: https://msdn.microsoft.com/en-us/query-bi/m/power-query-m-language-specification
  11.  
  12. As always my sessions may be a whirlwind but I want you to know - It's not about how much you know, it's about how much you know is POSSIBLE! Please feel free to connect with me online via LinkedIn and if you're stuck in the data trenches drop me a line at [email protected]
  13.  
  14. Bonus:
  15. If you wish to follow along for the first example on your own time, I have a video on YouTube where I covered the Food Composition Database:
  16. https://www.youtube.com/watch?v=LwIod7Dm46s&t=931s
  17.  
  18. Thanks again to all who attended!
  19.  
  20. - Alex Powers -
  21.  
  22.  
  23.  
  24.  
  25. Example 1: Food Composition Database
  26. From Web: https://ndb.nal.usda.gov/ndb/search/list
  27. Instructions: Insert A Food Value
  28. Challenge: Determine the total pages by traversing the DOM (Document Object Model) and affix the "&offset=" query string to iterate over all the pages.
  29.  
  30.  
  31. (food as text) =>
  32. // Returns Page and Offset Values
  33. let
  34. Source = Web.Page(
  35. Web.Contents("https://ndb.nal.usda.gov/ndb/search/list?qlookup=" & food)
  36. ),
  37. Data0 = Source{0}[Data]
  38. in
  39. Data0
  40.  
  41.  
  42. //
  43.  
  44. Example 2: Yelp Reviews
  45. From Example: https://www.yelp.com/biz/the-black-thorn-pub-and-pizza-saint-louis
  46. Instructions: Insert the business name as it appears on the URL page path
  47. Ex. https://www.yelp.com/biz/the-black-thorn-pub-and-pizza-saint-louis
  48. Pass Through: the-black-thorn-pub-and-pizza-saint-louis
  49.  
  50. (#"Business Name" as text) =>
  51. let
  52. Yelp = "https://www.yelp.com/biz/",
  53. Source = Web.BrowserContents(Yelp & #"Business Name"),
  54. #"Total Pages" = Table.FromList(List.Numbers(0, Number.From(Text.BetweenDelimiters(Source, "review-count rating-qualifier""" & ">", " reviews")) / 20, 20), Splitter.SplitByNothing(), {"Page Offset"}, {}, ExtraValues.Error),
  55. #"Added Custom" = Table.AddColumn(#"Total Pages", "Page Results", each Html.Table(Web.BrowserContents(Yelp & #"Business Name" & "?start=" & Text.From([Page Offset])), {{"Review", "[itemtype=""http\:\/\/schema\.org\/Review""]"}, {"Review Date", ".review-content > .biz-rating"}})),
  56. #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Page Results"}),
  57. #"Expanded Page Results" = Table.ExpandTableColumn(#"Removed Other Columns", "Page Results", {"Review Date", "Review"}, {"Review Date", "Review"}),
  58. #"Extract Date" = Table.AddColumn(#"Expanded Page Results", "Date", each Text.Start([Review Date], 10), type text),
  59. #"Removed Columns" = Table.RemoveColumns(#"Extract Date",{"Review Date"}),
  60. #"Clean Review Text" = Table.TransformColumns(#"Removed Columns",{{"Review", Text.Clean, type text}}),
  61. #"Changed Type" = Table.TransformColumnTypes(#"Clean Review Text",{{"Date", type date}})
  62. in
  63. #"Changed Type"
  64.  
  65. //
  66.  
  67. Example 3: Recursive Function
  68. Google Search Query: https://www.google.com/search?q=
  69. Instructions: Pass through the search value to return the first page URL's from Google's search query.
  70. Ex. Cats (There's going to be a lot)
  71. Challenge: Determine total number or results and create a list to paginate the results
  72.  
  73. (Search as text) =>
  74. let
  75. Source = Text.FromBinary(Web.Contents("http://www.google.com/search?q=" & Search)),
  76.  
  77. getHyperlink = (counter as number) =>
  78. let
  79. hyperlink = Text.BetweenDelimiters(Source, "/url?q=", "&amp", counter)
  80. in
  81.  
  82. /* Where Recursive Function Begins - with the if statement we offer ourselves a way out of if a blank string is returned
  83. otherwise we continue to increment through the page where the text between the delimiters is "/url?q=" and "&amp" */
  84.  
  85. if hyperlink = "" then {}
  86. else List.Combine({{hyperlink}, @getHyperlink(counter + 1)}),
  87. Output = getHyperlink(0)
  88. in
  89. Output
Advertisement
Add Comment
Please, Sign In to add comment