【问题标题】:Getting ... yelp_soup is not defined when writting scraper script编写刮板脚本时未定义... yelp_soup
【发布时间】:2017-11-11 18:57:53
【问题描述】:

我收到了错误:

编写爬虫脚本时没有定义yelp_soup

我不知道为什么,这是从 udemy 课程复制的代码,但它适用于他们。这是为什么呢?

这是 github 的链接,我从那里复制它作为最后的手段,但不起作用:

https://github.com/codingforentrepreneurs/30-Days-of-Python/blob/master/Day%2021%20-%2023/scrape/code/scape.py

有什么建议吗?我尝试了一些想法,但没有任何改变?

【问题讨论】:

    标签: python python-3.x web-scraping yelp


    【解决方案1】:

    为了达到这个目的,我做了一些修改。运行它,如果您遇到更多问题,请告诉我:

    import requests
    from bs4 import BeautifulSoup
    
    base_url = 'https://www.yelp.com/search?find_desc=Restaurants&find_loc='
    loc = 'Newport+Beach,+CA'
    page = 10
    
    url = base_url + loc + "&start=" + str(page)
    yelp_soup = BeautifulSoup(requests.get(url).text, 'lxml')
    
    for biz in yelp_soup.find_all(class_='biz-listing-large'):
        title = biz.find_all(class_='biz-name')[0].text
        address = biz.select('address,.biz-parent-container')[0].text
        phone = biz.find_all(class_='biz-phone')[0].text
        print(title.strip(),address.strip(),phone.strip())
    

    【讨论】:

    • 为什么我还在。名称 yelp_soup 未定义?仍然出现错误:/
    • 您确定已安装requestsBeautifulSoup 库吗?
    • 我现在做到了。我删除了整个文件夹,重做所有内容,再次重新安装了库,现在它可以完美运行了。不知道为什么
    • 如果有效,请确保通过勾选我的答案旁边的复选标记来接受它作为答案。谢谢。
    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多