I am writing code to scrape learning data from cookpad when doing machine learning in Python. At that time, if you tried to declare global from within the function, SyntaxError occurred. The error message is as follows.Error message
File "getURLbc.py", line 46 global fir_link SyntaxError: name 'fir_link' is used prior to global declaration
The code is as follows. I think it's very verbose and difficult to read, but please forgive me for being a beginner.
### #coding: UTF-8 import requests from bs4 import BeautifulSoup import time URLs =  sec_link =  thi_link =  count = 1 fo = open (r'recipes \ meat \ allrecipes.txt ',' a ') print ('Please enter the category top URL') fir_link = str (input ()) def URLget (): for x in range (10000): r = requests.get (fir_link) soup = BeautifulSoup (r.content, "html.parser") for a in soup.find_all ('a'): sec_link.append (a.get ('href')) for i in sec_link: if 'recipe' in str (i): thi_link.append (i) URL = [i for i in thi_link if (i.replace ('/ recipe /', '')). Isdigit () == True] c_URL =  for i in URL: c_URL.append ('https: //cookpad.com'+str (i)) URL_set = set (c_URL) URL_list = list (URL_set) global URLs URLs = URL_list key = soup.find ('a', class _ = 'next_page') key2 = key.get ('href') global fir_link fir_link = 'https: //cookpad.com'+str (key2) print (str (count) + 'The first acquisition was successful') global count count + = 1 time.sleep (5) fo.write (URLs) URLget ()
This may be a very rudimentary question, but please give me an answer.
When this code was used to scrape about two weeks ago, the above error did not occur. However, because it became necessary to collect data again, an error occurred when I tried to use it again. During the two weeks, I installed anaconda3 and changed the development environment a little, so I thought it was the cause and uninstalled and tried again, but I could not solve it.Supplemental information (FW/tool version etc.)
Answer # 1
It may be a good idea to declare a global variable to be used in the function immediately after
def URLget (): global ...
Answer # 2
SyntaxError: name'fir_link'is used prior to global declaration
"Global variable used before global declaration"
is what it means.
Declare global before using it.
Answer # 3
r = requests.get (fir_link)
This fir_link is not global
- python - list index out of range error occurs for index 0
- python - django noreversematch occurs only on a specific page
- error occurs in getelement method of htmlobject obtained from win32comclient in python37
- i am making a client server system with python, but there are some parts that are not displayed in the gui
- python - i want to correct the location that causes syntaxerror: invalid syntax
- python - an error occurs when reading 3 or more files in machine learning
- python - i don't know the cause of syntaxerror: invalid syntax
- python 3x - nosuchelementexception exception occurs when trying to get by id with python selenium
- python - unicodedecodeerror(utf-8) or schemaerror(shift-jis) occurs when outputting a shapefile using geopandas
- python - what to do when a modulenotfounderror occurs
- python - [scikit-learn] an error occurs when calculating the evaluation index
- attributeerror occurs when importing matplotlib in python
- python - i want to solve the error that occurs in learning scikit-learn
- i want to get rid of python global variables
- python - why an attributeerror occurs
- python - an error occurs in the account creation process using [django]formview
- python - [error 5] access is denied: occurs in osmakedirs
- python - syntaxerror: invalid syntax is displayed
- python - i tried to make a multiplication table with the while function, but an error occurs, so please tell me the correction p
- are there any programming ideas you should learn to learn python?
- object oriented in python tkinter
- python - [natural language processing] regarding back propagation of embedding layer when word2vec is implemented
- python - modelpredict() cannot be executed in tensorflow
- python csv even odd sorting method
- python - i want to change optional arguments according to conditions
- i want to replace every other character using python regular expression
- call function dynamically in python
- i want to check python files
- python - regarding the problem of replacing unintended characters when replacing strings