简体   繁体   English

获取 NameError:未定义名称“请求”

[英]Getting NameError: name 'requests' is not defined

I have already imported requests in def and ran it.我已经在 def 中导入了请求并运行了它。 But still got a name error... I import all items at a time and sum up all function into one.但仍然出现名称错误...我一次导入所有项目并将所有功能汇总为一个。

    #import all the libraries
    def import_all_modules():
      from bs4 import BeautifulSoup as soup
      import requests 
      import pandas as pd
      from google.colab import drive
    
    #Get
    def get_html_from_url(url:str):
      html=requests.get(url).content
      return html
    
    
    #load the html content
    def load_page_and_filter(html):
      soup_page=soup(html, "html")    
      shoes=soup_page.find_all("div",{"class":"good-box"})
      return shoes
    
    #Create Pandas DataFrame from HTML
    def generate_detaframe_from_soup(soup):
      names=[]
      prices=[]
      for shoe in soup:
        names.append(shoe.a.span.text)
        prices.append(shoe.div.p.text)
      
      adidas_shoes_dict={
          "Name":names,
          "Price":prices
      } 
      df= pd.DataFrame(data=adidas_shoes_dict)
      df["Price"]=df["Price"].apply(lambda x: float(x.split("\xa0")[-1]))
      return df
      
    #save CSV
    def save_csv(file_name, df):
      drive.mount("/content/drive")
      df.to_csv(file_name)

run all the method at once and put them in one function一次运行所有方法并将它们放在一个函数中


    def run_web_scraping(url,file_name):
      import_all_modules()
      html=get_html_from_url(url)
      soup=load_page_and_filter(html)
      df=generate_detaframe_from_soup(soup)
      save_csv(file_name, df)

saved URL and file_name for run_web_scrapping为 run_web_scrapping 保存的 URL 和 file_name


    url="https://www.adidas.com.hk/men/shoes/basketball"
    file_name="/content/drive/MyDrive/adidas.csv"
    run_web_scraping(url,file_name)

Imports are binded into the current scope, so if you do your imports in a function, they will not be available once you exit the function.导入绑定到当前作用域中,因此如果您在函数中进行导入,一旦退出该函数,它们将不可用。

Just put them at the top of the file, not in a function, and it should work great.只需将它们放在文件的顶部,而不是放在函数中,它应该可以很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 已经安装请求(使用 JuypterLab)后获取“NameError:名称'请求'未定义” - Getting “NameError: name 'requests' is not defined” after already installing requests (using JuypterLab) 获取 NameError:名称“countVectorizer”未在 Pycharm 中定义 - Getting NameError: name 'countVectorizer' is not defined in Pycharm 获取“ NameError:名称&#39;room_path&#39;未定义” - Getting “NameError: name 'room_path' is not defined” 我收到 NameError: name &#39;n&#39; is not defined - I am getting a NameError: name 'n' is not defined 出现名称错误 NameError,函数未定义? - Getting a name error NameError, function is not defined? 获取 NameError:名称“视频”未在 Vapoursynth/Python 中定义 - Getting NameError: name 'video' is not defined in Vapoursynth/Python 收到错误“ NameError:未定义名称&#39;Tk&#39;” - Getting error “NameError: name 'Tk' is not defined” 获取“ NameError:名称” <my function> “未定义”错误 - Getting 'NameError: name '<my function>' is not defined' error 在python中获取未从NameError定义的名称 - Getting the name which is not defined from NameError in python 调用exec,虽然名称已定义,但仍会获取NameError - Calling exec, getting NameError though the name is defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM