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 - what to do when a modulenotfounderror occurs
- python - i am having a problem because a list index out of range error occurs at the intersection of the onemax problem
- python 3x - about python global variables
- python - nameerror occurs when reading a function from another file
- python - what to do when an applicationerror occurs on heroku
- python - an error occurs in the fomat method of print
- a syntaxerror occurred in the if else statement of python how can i fix it?
- How to understand global variables in Python
- python 3x - an error occurs when the datetime type range is specified in the condition
- python - i am making a setting part of the function to start automatically, but it does not work
- python - fielddoesnotexist occurs when using uniqueconstraints of django model definition
- python: i want to change the position by making the y-axis title vertical
- python - unicodedecodeerror(utf-8) or schemaerror(shift-jis) occurs when outputting a shapefile using geopandas
- python - an error occurs in the contour extraction program
- misalignment occurs when outputting python list to csv
- i'm making a system that sounds a siren at a specified time with python, but attributeerror occurs with zfill
- 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 - if a specific button (eg root) is pressed, execute the process that was stopped
- python: how to change the print format of the answer obtained with return
- python - order django admin categories alphabetically
- python - i'm in trouble because i don't understand atcoder146d
- python - unicodedecodeerror when installing pip on windows
- python - missing 1 required positional argument
- python - i created an ssl authentication detection program, but an exception occurred
- python - reason why certificate information is returned even though it is not https
- python - collect data every n seconds with pandas and process for each column
- how to use boolean type in python