0

I was trying out this Python code from a training website in IPython:

from bs4 import BeautifulSoup

import requests

url = raw_input("www.google.com")

r  = requests.get("http://" +url)

data = r.text

soup = BeautifulSoup(data)

for link in soup.find_all('a'):
    print(link.get('href'))

and found that it ran fine on the first try. I've now tried simply restarting the kernel, opening a new notebook, and generally returning the settings to how they were when I first ran the program with no luck. Why might IPython be failing to run the code and giving no response at all (as though I haven't clicked anything)?

1 Answer 1

1

Seems like raw_input is not supported by IPython. So it's probably just hanging there. If you change:

url = raw_input("www.google.com")

to

url = "www.google.com"

it should work.

Sign up to request clarification or add additional context in comments.

3 Comments

When I try that I get an 'invalid syntax' error. I'm also skeptical because it did work the first time for some reason with the bracket syntax. Thanks though!
@MaxPower So you're saying on your first successful run it actually prompted you for input? The first argument to raw_input is the prompt text. So "www.google.com" doesn't really make much sense there. Something like "Enter URL for link extraction:" would make more sense.
You're right. I was completely misreading the code. That was pretty silly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.