Wrong output in python3 via terminal on mac - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Wrong output in python3 via terminal on mac (/thread-11142.html) |
Wrong output in python3 via terminal on mac - obligato - Jun-24-2018 Hello, So I'm testing out a web parser which takes numbers off a webpage and finds the total. However, each time I run the code it keeps asking me to enter file name while there is no input line in my file. Kindly assist to investigate what could be wrong. import urllib.request #from bs4 import BeautifulSoup import BeautifulSoup url = 'http://py4e-data.dr-chuck.net/comments_42.html' html = urllib.request.urlopen(url).read() soup = BeautifulSoup(html, "html.parser") # Retrieve all of the anchor tags tags = soup('a') for tag in tags: # Look at the parts of a tag y = re.findall('[0-9]+', tag.text) for v in y: #count = count + 1 total = total + int(v) #print('count is', count) print(('total is ',total)) RE: Wrong output in python3 via terminal on mac - Larz60+ - Jun-24-2018 for your import, use: from bs4 import BeautifulSoup RE: Wrong output in python3 via terminal on mac - snippsat - Jun-25-2018 (Jun-24-2018, 11:02 PM)obligato Wrote: However, each time I run the code it keeps asking me to enter file name while there is no input line in my file.It should not do that,try running from Terminal command line if you run from a IDE/editor. Even if fix import the code will never work because tags = soup('a') ,there are no links(which a search for) on that page.To show a example that work. import urllib.request from bs4 import BeautifulSoup url = 'http://py4e-data.dr-chuck.net/comments_42.html' html = urllib.request.urlopen(url) soup = BeautifulSoup(html, "html.parser") td_tag = soup.find_all('span', class_="comments") total = sum(int(td.text) for td in td_tag) print('total is {}'.format(total)) Should also drop urllib and use Requests.
|