Home>

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.

Tried

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.)

Python 3.7.0

  • Answer # 1

    It may be a good idea to declare a global variable to be used in the function immediately after

    def.

    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