How To Get An Html File Using Python?
I am not very familiar with Python. I am trying to extract the artist names (for a start :)) from the following page: http://www.infolanka.com/miyuru_gee/art/art.html. How do I re
Solution 1:
Example using urlib and lxml.html:
import urllib
from lxml import html
url = "http://www.infolanka.com/miyuru_gee/art/art.html"
page = html.fromstring(urllib.urlopen(url).read())
for link in page.xpath("//a"):
print"Name", link.text, "URL", link.get("href")
output >>
[('Aathma Liyanage', 'athma.html'),
('Abewardhana Balasuriya', 'abewardhana.html'),
('Aelian Thilakeratne', 'aelian_thi.html'),
('Ahamed Mohideen', 'ahamed.html'),
]
Solution 2:
I think "eyquem" way would be my choice too, but I like to use httplib2 instead of urllib. urllib2 is too low level lib for this work.
import httplib2, re
pat = re.compile('<DT><a href="[^"]+">(.+?)</a>')
http = httplib2.Http()
headers, body = http.request("http://www.infolanka.com/miyuru_gee/art/art.html")
li = pat.findall(body)
print li
Solution 3:
Use urllib2 to get the page.
Use BeautifulSoup to parse the HTML (the page) and get what you want!
Solution 4:
Check this my friend
import urllib.request
import re
pat = re.compile('<DT><a href="[^"]+">(.+?)</a>')
url = 'http://www.infolanka.com/miyuru_gee/art/art.html'
sock = urllib.request.urlopen(url).read().decode("utf-8")
li = pat.findall(sock)
print(li)
Solution 5:
Or go straight forward:
import urllib
import re
pat = re.compile('<DT><a href="[^"]+">(.+?)</a>')
url = 'http://www.infolanka.com/miyuru_gee/art/art.html'
sock = urllib.urlopen(url)
li = pat.findall(sock.read())
sock.close()
print li
Post a Comment for "How To Get An Html File Using Python?"