0

I am trying to read the HTML contents of a URL with Python. To fetch the HTML contents of a URL, would I use the module wget, urllib or a different module entirely?

After Answers: I will use the urllib module since that comes with the default Python 2.7 build, and I can't download external modules from this computer.

List of Modules That Fetch URL Contents:

Wget
Beautiful Soup
Urllib
Requests
5
  • Have you tried BeautifulSoup? Commented Oct 4, 2016 at 16:50
  • Yes. There are multiple choices (add requests to the list) and you can use any of them. Commented Oct 4, 2016 at 16:50
  • Can you show us what you've tried? There are a lot of questions and answers on this site about this topic. Do you have a specific question? Commented Oct 4, 2016 at 16:51
  • @MooingRawr I will upload some code soon. I'll try my 1st attempt at fetching html elements from a URL with wget and post it once I'm done. Commented Oct 4, 2016 at 16:55
  • @345243lkj I have not tried BeautifulSoup, but I will look into it, and decide if it's the module I should use for this task. Commented Oct 4, 2016 at 16:56

1 Answer 1

5

Here is a sample to get you started with requests:

import requests

resp = requests.get('http://httpbin.org/get')
if resp.ok:
    print (resp.text)
else:
    print ("Boo! {}".format(resp.status_code))
    print (resp.text)
Sign up to request clarification or add additional context in comments.

3 Comments

This is the simplest. Replace print resp.text with print(resp.text) if you're using Python3.x
Aside: httpbin.org is an excellent resource for testing clients.
Thanks, @345243lkj - I fixed the sample and the comment.

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.