Feb-28-2020, 05:58 AM
I'm wanting to switch to PyCharm but I'm getting a Traceback error with the following code that I haven't gotten before in other IDEs. I'm running v3.8.2 on a Mac. Any help would be greatly appreciated!
# import libraries
from urllib.request import urlopen
from bs4 import BeautifulSoup
# specify the url
url = "https://www.bbc.com/sport/football/46897172"
# Connect to the website and return the html to the variable ‘page’
try:
page = urlopen(url)
except:
print("Error opening the URL")
# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page, 'html.parser')
# Take out the <div> of name and get its value
content = soup.find('div', {"class": "story-body sp-story-body gel-body-copy"})
article = ''
for i in content.findAll('p'):
article = article + ' ' + i.text
print(article)
# Saving the scraped text
with open('scraped_text.txt', 'w') as file:
file.write(article)Error:Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm CE.app/Contents/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/dv/Desktop/Test/test.py", line 15, in <module>
soup = BeautifulSoup(your_mom, 'html.parser')
NameError: name 'page' is not defined
