def parse(self, response): recipeDescription = response.css("article.recipe").css('meta[itemprop="description"]').xpath("@content") recipe = response.css("div.recipe-callout") if recipe: ingredientList = response.xpath('//li[@class="ingredient"]//text()').extract() ingredientObjects = [] for i in ingredientList: ingredientObjects.append(parse(i)) print parse(i) yield RecipeItem(name = recipe.css("h2::text").extract_first(), urlName = response.url, imgURL = response.css("div.entry-content").css("div.featured-image").css("img.photo").xpath("@src").extract_first(), ingredients = ingredientObjects, ingredientsRawString = ingredientList, directions = response.css('div[itemprop="recipeInstructions"]').css('p::text').extract(), description = response.css("article.recipe").css('meta[itemprop="description"]').xpath("@content").extract_first(), cuisine = response.xpath('////*[@id="content"]/div[1]/article/footer/div/span[2]/span/a/span/text()').extract(), prepTime = response.css("span.cooktime").xpath("@content").extract_first() ) else: links = response.css("li.recipe").css("a").xpath("@href").extract() if links: for link in links: yield scrapy.Request(link, callback=self.parse) # If the current page is a navigation page, goes through next page # link until there are no more recipes to scrape nextLink = response.css("a.page-numbers.next").xpath("@href").extract_first() if nextLink: yield scrapy.Request(nextLink, callback=self.parse)
def test_parse(self): parsed_query = parse("one kilogram red meat") print(parsed_query) self.assertEqual(parsed_query['measure'], "one kilogram") self.assertEqual(parsed_query['unit'], "kilogram") self.assertEqual(parsed_query['quantity'], "one") self.assertEqual(parsed_query['name'], "red meat")
def getRandom(): if (len(randomList) > 0): print random.choice(randomList) return json.dumps(random.choice(randomList)) else: s = "https://api.edamam.com/search?q=Italian" s += "&app_id=" + keys.appID() + "&app_key=" + keys.appSecret() print s webUrl = urllib2.Request(url=s) webUrl = urllib2.urlopen(webUrl) if (webUrl.getcode() == 200): results = [] counter = 0 response = webUrl.read().decode('utf-8') theJSON = json.loads(response) for i in range(0, len(theJSON['hits'][0]['recipe']['ingredients'])): results.append( parse(theJSON['hits'][0]['recipe']['ingredients'][i] ['text'])['name']) url = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases" postReq = [] counter = 0 for i in range(0, len(results)): postReq.append({ "language": "en", "id": str(counter), "text": results[i].encode('utf-8') }) counter += 1 payload = str({"documents": postReq}) headers = { 'content-type': "application/json", 'ocp-apim-subscription-key': str(keys.azureKey()) } response = requests.request("POST", url, data=payload, headers=headers) response = response.json() for i in range(0, len(response['documents'])): userIngredients.append( parse(response['documents'][i]['keyPhrases'][0])['name']) userChoices.append(theJSON['hits'][0]) randomList.append(theJSON['hits'][1]) randomList.append(theJSON['hits'][2]) return json.dumps(theJSON['hits'][0])
from ingredient_parser.en import parse print(parse('2 liters of milk'))
from ingredient_parser.en import parse print(parse('1/4 cup dried white pea beans').get("measure")) # from recipe_searchers import search_recipe # result = search_recipe("12 ounces lean ground beef, preferably 85 percent lean") # print(f"Found results: \n {result}") my_string = "hello python world , i'm a beginner " print(my_string.split(",", 1)[1]) print(my_string.split("'", 1)[1])