-2

EDIT: Still getting the error (the script executed once but the output was blank)

I get the following error when trying to run this script python scraping from the CMD:

Microsoft Windows [Version 10]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Far\Desktop\Coding>python scraping_imdb_episodes.py
Traceback (most recent call last):
  File "C:\Users\Far\Desktop\Coding\scraping_imdb_episodes.py", line 7, in <module>
    response = get('https://www.imdb.com/title/tt1439629/episodes?season=' + str(sn))
NameError: name 'get' is not defined

I searched for the solution here and here but don't grasp what's going wrong.

Minimal Reproducible Example:

# Initializing the series that the loop will populate
community_episodes = []

# For every season in the series-- range depends on the show
for sn in range(1,7):
    # Request from the server the content of the web page by using get(), and store the server’s response in the variable response
    response = get('https://www.imdb.com/title/tt1439629/episodes?season=' + str(sn))

    # Parse the content of the request with BeautifulSoup
    page_html = BeautifulSoup(response.text, 'html.parser')

    # Select all the episode containers from the season's page
    episode_containers = page_html.find_all('div', class_ = 'info')

    # For each episode in each season
    for episodes in episode_containers:
            # Get the info of each episode on the page
            season = sn
            episode_number = episodes.meta['content']
            title = episodes.a['title']
            airdate = episodes.find('div', class_='airdate').text.strip()
            rating = episodes.find('span', class_='ipl-rating-star__rating').text
            total_votes = episodes.find('span', class_='ipl-rating-star__total-votes').text
            desc = episodes.find('div', class_='item_description').text.strip()
            # Compiling the episode info
            episode_data = [season, episode_number, title, airdate, rating, total_votes, desc]

            # Append the episode info to the complete dataset
            community_episodes.append(episode_data)

I just tested get and request.get on this Online Compiler and still got the errors:

Traceback (most recent call last):
  File "<string>", line 7, in <module>
NameError: name 'get' is not defined

get error

Traceback (most recent call last):
  File "<string>", line 7, in <module>
NameError: name 'requests' is not defined

requests error

Lod
  • 657
  • 1
  • 9
  • 30
  • 1
    you have to use do `response = requests.get(url)` – sunil ghimire Dec 22 '21 at 09:36
  • 1
    Please include a [mre] demonstrating your problem. – khelwood Dec 22 '21 at 09:37
  • 1
    The snippet says ***"partial code"*** at the top. How exactly to fetch the contents of the URL is sort of omitted here, i.e. it's "bring your own `get`". The rest of the code is supposed to illustrate what to do with the content once you've fetched it. – deceze Dec 22 '21 at 09:39
  • Thanks a lot guys. I just installed requests and now it's running ok. Didn't know we needed it. – Lod Dec 22 '21 at 09:57
  • @khelwood thanks for the suggestion. The example was given in the 1st link https://gist.github.com/isabellabenabaye/960b255705843485698875dc193e477e . – Lod Dec 22 '21 at 09:58
  • @Lod A link is not a [mre]. Code required to understand your problem should be included in your question. – khelwood Dec 22 '21 at 10:08
  • @khelwood ok. Do you mean pasting the code from the link as valid minimal reproducible example? plus the error code? Thanks. – Lod Dec 22 '21 at 10:18
  • 1
    Yes, the mre should be included in a code block in your question, and error messages or output should be included in your question. – khelwood Dec 22 '21 at 10:28
  • 1
    If you want to use `requests.get`, then you need `import requests`. If you want to use just `get` then you need `from requests import get`. – khelwood Dec 22 '21 at 10:45
  • @khelwood thanks. Here I got both `requests.get` with `import requests` https://i.imgur.com/spfYXWl.png and `get` with `from requests import get` https://i.imgur.com/7WI4auB.png but the errors remain. What am I not doing right? Thanks. – Lod Dec 22 '21 at 11:07
  • 2
    You need to `import requests` **in your .py file**, not just type it into a console and then run the file separately. – deceze Dec 22 '21 at 11:09
  • @deceze thanks a lot for the pointer! I didn't know about that. I added `from bs4 import BeautifulSoup` and `import requests` as first 2 lines of the py script and now I'm not getting any more errors. Thanks again. Be well! – Lod Dec 22 '21 at 11:44
  • @deceze I got it sorted finally here: https://pastebin.com/fCh3sGPK Many thanks again guys. – Lod Dec 22 '21 at 14:57
  • [Images](https://meta.stackoverflow.com/q/285551/90527) should not be used for textual data, such as code and errors. – outis Dec 23 '21 at 10:40
  • @outis thanks for letting me know. – Lod Dec 24 '21 at 12:36
  • @outis yes it's a similar solution thanks. I did end up solving it by putting `from bs4 import BeautifulSoup` and `import requests` at the top of the script as @deceze pointed me to in previous comment. Be well! – Lod Dec 24 '21 at 14:43

1 Answers1

1

"get" is a method from requests library. Make sure that you have imported get from requests or else use

response = requests.get('https://www.imdb.com/title/tt1439629/episodes?season=' + str(sn))
Faheem Sharif
  • 246
  • 2
  • 6
  • Thanks a lot @faheem Sharif. I just installed requests and now it's running ok. Didn't know we needed it. – Lod Dec 22 '21 at 09:57
  • I just add the final code link here (shared in previous comment) so others can find it quickly for reference: https://pastebin.com/fCh3sGPK Many thanks again! – Lod Dec 24 '21 at 12:49