【问题标题】:BeautifulSoup web scraping: UnboundLocalError: local variable 'soup' referenced before assignmentBeautifulSoup 网页抓取:UnboundLocalError:分配前引用的局部变量“汤”
【发布时间】:2021-09-12 21:01:00
【问题描述】:

我尝试用漂亮的汤和请求通过 Youtube 视频进行网络抓取,一切都很顺利,直到我遇到这个错误,但它为导师解决了。

import requests
from bs4 import BeautifulSoup

def get_data(url):
    if not response.ok:
        print('Server Responded: {}'.format(response.status_code))
    else:
        soup = BeautifulSoup(response.text, 'lxml')
    return(soup)

def get_detail_data(soup):
    try:
        title = soup.find('h1', id='itemTitle').text.strip()
    except:
        title = ''
        
    try:
        p = soup.find('span', id='prcIsum').text.strip()
        currency, price = p.split(' ')
    except:
        currency = ''
        price = ''
    
    try:
        sold = soup.find('span', class_='vi-qtyS-hot-red').a.text.strip().split(' ')[0]
    except:
        sold = ''
    
    data = {
        'title' : title,
        'currency' : currency,
        'price' : price,
        'total units sold' : sold
    }

    return data

def get_index_data(soup):
    try:
        links = soup.find_all('a', class_='s-item__link')
    except:
        links = []

    
    urls = [item.get('href') for item in links]
    return urls

def main():
    url = 'https://www.ebay.com/sch/i.html?_nkw=mens+shoes&_sacat=0'
    
    products = get_index_data(get_data(url))

    for link in products:
        data =  get_detail_data(get_data(link))


if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup python-requests


    【解决方案1】:

    在 get_data 中,您缺少发出实际请求并存储响应。然后,如果 response.ok 不是 True,则需要指定 soup = None。最后,在其他地方你需要在尝试调用它的方法之前测试soup 是否为None。

    import requests
    from bs4 import BeautifulSoup
    
    def get_data(url):
        
        response = requests.get(url, headers = {'User-Agent':'Mozilla/5.0'}) #this was missing
        
        if not response.ok:
            print('Server Responded: {}'.format(response.status_code))
            soup = None
        else:
            soup = BeautifulSoup(response.text, 'lxml')
        return soup
    
    def get_detail_data(soup):
        
        try:
            title = soup.find('h1', id='itemTitle').text.strip()
        except:
            title = ''
            
        try:
            p = soup.find('span', id='prcIsum').text.strip()
            currency, price = p.split(' ')
        except:
            currency = ''
            price = ''
        
        try:
            sold = soup.find('span', class_='vi-qtyS-hot-red').a.text.strip().split(' ')[0]
        except:
            sold = ''
        
        data = {
            'title' : title,
            'currency' : currency,
            'price' : price,
            'total units sold' : sold
        }
    
        return data
    
    def get_index_data(soup):
        try:
            links = soup.find_all('a', class_='s-item__link')
        except:
            links = []
    
        
        urls = [item.get('href') for item in links]
        return urls
    
    def main():
        
        url = 'https://www.ebay.com/sch/i.html?_nkw=mens+shoes&_sacat=0'
        soup = get_data(url)
        
        if not soup is None:
            
            products = get_index_data(soup)
            #print(products)
    
            for link in products:
                
                soup = get_data(link)
                
                if not soup is None:
                    
                    data =  get_detail_data(soup)
                    print(data)
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 你试过运行这个吗?
    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 2020-01-16
    • 2019-12-05
    • 2017-09-19
    • 2020-09-26
    • 2022-01-02
    • 2019-03-22
    • 2019-01-24
    相关资源
    最近更新 更多