Finding the names and phones of interesting businesses

from urllib.request import urlopen from lxml import html from urllib.parse import quote_plus # Python3 what, where = 'Pancakes', 'San Francisco' # or 'Restaurants', 'Chicago'; 'Coffee & tea', 'Palo Alto' base = 'https://www.yelp.com/search' query = 'find_desc={0}&find_loc={1}'.format(quote_plus(what), quote_plus(where)) url = '{0}?{1}'.format(base, query) content = urlopen(url).read() tree = html.fromstring(content) for item in tree.xpath('//li[@class="regular-search-result"]'): business = item.xpath('.//span[@class="indexed-biz-name"]/a/span/text()') stars = item.xpath('.//div[contains(@class, "i-stars")]/@title') reviews = item.xpath('.//span[contains(@class, "review-count")]/text()') phone = item.xpath('.//div[@class="secondary-attributes"]/span[@class="biz-phone"]/text()') business, stars, reviews, phone = [item[0].strip() for item in (business, stars, reviews, phone)] print('{business} ({stars}, {reviews}): {phone}'.format( business = business, stars = stars, reviews = reviews, phone = phone )) """ House Of Pancakes (4.0 star rating, 629 reviews): (415) 681-8388 Dottie’s True Blue Cafe (4.0 star rating, 3622 reviews): (415) 885-2767 Griddle Fresh (4.0 star rating, 772 reviews): (415) 647-7037 Plow (4.0 star rating, 1989 reviews): (415) 821-7569 Zazie (4.0 star rating, 3731 reviews): (415) 564-5332 Mymy (4.0 star rating, 1281 reviews): (415) 800-7466 Millbrae Pancake House (4.0 star rating, 1981 reviews): (650) 589-2080 Sweet Maple (4.0 star rating, 2505 reviews): (415) 655-9169 Mama’s On Washington Square (4.0 star rating, 3666 reviews): (415) 362-6421 RAW Sugar Factory (4.0 star rating, 120 reviews): (415) 872-9608 """