Basic example with for loop

from urllib2 import *
ur = urlopen("")#open url
contents = ur.readlines()#readlines from url file
fo = open("test.txt", "w")#open test.txt
for line in contents: 
    print "writing %s to a file" %(line,)
    fo.write(i)#write lines from url file to text file
fo.close()#close text file

urlopen() does not seem to work for me, as in I cannot import it. I am using Python 3.4.3 though.

Edited 1 Year Ago by Jalexmaines: I made a typo.

Here are the diffrent ways,
and also what i would call the prefered way these day with Requests.

Python 2:

from urllib2 import urlopen

page_source = urlopen("").read()
print page_source

Python 3:

from urllib.request import urlopen

page_source = urlopen('').read().decode('utf_8')

For Python 3 to get str output and not byte we need to decode to utf-8.

Here with Requests,work for Python 2 and 3:

import requests

page_source = requests.get('')

Basic web-scraping we read in with Requests and parse with BeautifulSoup.

import requests
from bs4 import BeautifulSoup    

page_source = requests.get('')
soup = BeautifulSoup(page_source.text)
print(soup.find('title').text) #--> Welcome to

Edited 1 Year Ago by snippsat

This question has already been answered. Start a new discussion instead.