{"id":28180,"date":"2022-12-29T04:43:52","date_gmt":"2022-12-28T23:13:52","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/"},"modified":"2022-12-29T04:43:52","modified_gmt":"2022-12-28T23:13:52","slug":"solved-scrapy-python-crawls-but-does-not-scrape-data","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/","title":{"rendered":"[Solved] scrapy\/Python crawls but does not scrape data"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-43323418\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"43323418\" data-parentid=\"43308210\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Your imports didn&#8217;t work that well over here, but that might be a configuration issue on my side. I think the scraper below does what you&#8217;re searching for:<\/p>\n<pre><code>import scrapy\n\nclass YelpSpider(scrapy.Spider):\n    name=\"yelp_spider\"\n    allowed_domains=[\"yelp.com\"]\n    headers=['venuename','services','address','phone','location']\n\n    def __init__(self):\n        self.start_urls = ['https:\/\/www.yelp.com\/search?find_desc=&amp;find_loc=Springfield%2C+IL&amp;ns=1']\n\n    def start_requests(self):\n        requests = []\n        for item in self.start_urls:\n            requests.append(scrapy.Request(url=item, headers={'Referer':'http:\/\/www.google.com\/'}))\n            return requests\n\n    def parse(self, response):\n        for restaurant in response.xpath('\/\/div[@class=\"biz-listing-large\"]'):\n            item={}\n            item['venuename']=restaurant.xpath('.\/\/h3[@class=\"search-result-title\"]\/span\/a\/span\/text()').extract_first()\n            item['services']=u\",\".join(line.strip() for line in restaurant.xpath('.\/\/span[@class=\"category-str-list\"]\/a\/text()').extract())\n            item['address']=restaurant.xpath('.\/\/address\/text()').extract_first()\n            item['phone']=restaurant.xpath('.\/\/span[@class=\"biz-phone\"]\/text()').extract_first()\n            item['location']=response.xpath('.\/\/input[@id=\"dropperText_Mast\"]\/@value').extract_first()\n            item['url']=response.url\n            yield item\n<\/code><\/pre>\n<p>Some explanation:<\/p>\n<p>I&#8217;ve changed the start url. This url actually provides an overview of all restaurants while the other did not (or at least not when viewed from my location).<\/p>\n<p>I&#8217;ve removed the pipeline as it was not defined in my system and I couldn&#8217;t try it out with the non-existent pipeline in the code.<\/p>\n<p>The parse function is the one I made the real changes to. The xpaths you defined weren&#8217;t very clear. Now the code loops over each listed restaurant.<\/p>\n<pre><code>response.xpath('\/\/div[@class=\"biz-listing-large\"]')\n<\/code><\/pre>\n<p>This code captures all the restaurants data. I&#8217;ve used this in a for loop, so we can perform actions for each restaurant. This data is available in the variable <code>restaurant<\/code>.<\/p>\n<p>So if I want to extract data from a restaurant, I use this variable. In addition, we need to start the xpath with a <code>.<\/code> because the script will otherwise start from the beginning of the webpage (which would be the same as using response).<\/p>\n<p>In order to understand the xpaths in my answer, I could explain this to you but there is a lot of documentation available and they&#8217;re probably better in explaining this than I am.<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.w3schools.com\/xml\/xml_xpath.asp\">Some documentation<\/a><\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms256086(v=vs.110).aspx\">And some more<\/a><\/p>\n<p>Note that I&#8217;ve used restaurant for most values of <code>item<\/code>. Values from location and url are not really restaurant data but are located elsewhere on the webpage. This is why those values use <code>response<\/code> instead of <code>restaurant<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">4<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved scrapy\/Python crawls but does not scrape data <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Your imports didn&#8217;t work that well over here, but that might be a configuration issue on my side. I think the scraper below does what you&#8217;re searching for: import scrapy class YelpSpider(scrapy.Spider): name=&#8221;yelp_spider&#8221; allowed_domains=[&#8220;yelp.com&#8221;] headers=[&#8216;venuename&#8217;,&#8217;services&#8217;,&#8217;address&#8217;,&#8217;phone&#8217;,&#8217;location&#8217;] def __init__(self): self.start_urls = [&#8216;https:\/\/www.yelp.com\/search?find_desc=&amp;find_loc=Springfield%2C+IL&amp;ns=1&#8217;] def start_requests(self): requests = [] for item in self.start_urls: requests.append(scrapy.Request(url=item, headers={&#8216;Referer&#8217;:&#8217;http:\/\/www.google.com\/&#8217;})) return requests def &#8230; <a title=\"[Solved] scrapy\/Python crawls but does not scrape data\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\" aria-label=\"More on [Solved] scrapy\/Python crawls but does not scrape data\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[349,2182,760,916],"class_list":["post-28180","post","type-post","status-publish","format-standard","hentry","category-solved","tag-python","tag-scrapy","tag-web-scraping","tag-xpath"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] scrapy\/Python crawls but does not scrape data - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] scrapy\/Python crawls but does not scrape data - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Your imports didn&#8217;t work that well over here, but that might be a configuration issue on my side. I think the scraper below does what you&#8217;re searching for: import scrapy class YelpSpider(scrapy.Spider): name=&quot;yelp_spider&quot; allowed_domains=[&quot;yelp.com&quot;] headers=[&#039;venuename&#039;,&#039;services&#039;,&#039;address&#039;,&#039;phone&#039;,&#039;location&#039;] def __init__(self): self.start_urls = [&#039;https:\/\/www.yelp.com\/search?find_desc=&amp;find_loc=Springfield%2C+IL&amp;ns=1&#039;] def start_requests(self): requests = [] for item in self.start_urls: requests.append(scrapy.Request(url=item, headers={&#039;Referer&#039;:&#039;http:\/\/www.google.com\/&#039;})) return requests def ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-28T23:13:52+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] scrapy\/Python crawls but does not scrape data\",\"datePublished\":\"2022-12-28T23:13:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\"},\"wordCount\":284,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"python\",\"scrapy\",\"web-scraping\",\"xpath\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\",\"name\":\"[Solved] scrapy\/Python crawls but does not scrape data - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-28T23:13:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] scrapy\/Python crawls but does not scrape data\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] scrapy\/Python crawls but does not scrape data - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] scrapy\/Python crawls but does not scrape data - JassWeb","og_description":"[ad_1] Your imports didn&#8217;t work that well over here, but that might be a configuration issue on my side. I think the scraper below does what you&#8217;re searching for: import scrapy class YelpSpider(scrapy.Spider): name=\"yelp_spider\" allowed_domains=[\"yelp.com\"] headers=['venuename','services','address','phone','location'] def __init__(self): self.start_urls = ['https:\/\/www.yelp.com\/search?find_desc=&amp;find_loc=Springfield%2C+IL&amp;ns=1'] def start_requests(self): requests = [] for item in self.start_urls: requests.append(scrapy.Request(url=item, headers={'Referer':'http:\/\/www.google.com\/'})) return requests def ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/","og_site_name":"JassWeb","article_published_time":"2022-12-28T23:13:52+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] scrapy\/Python crawls but does not scrape data","datePublished":"2022-12-28T23:13:52+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/"},"wordCount":284,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["python","scrapy","web-scraping","xpath"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/","url":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/","name":"[Solved] scrapy\/Python crawls but does not scrape data - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-28T23:13:52+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-scrapy-python-crawls-but-does-not-scrape-data\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] scrapy\/Python crawls but does not scrape data"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=28180"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/28180\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=28180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=28180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=28180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}