Python

champak
₹2,000.00 ₹300.00
The Python Programming Language at Champak's World
  • 22 students
  • 41 lessons
  • 0 quizzes
  • 10 week duration

Python

Downloading data from a remote website and displaying data in an html page

downloader.py


#Downloader
import requests
import urllib.request, urllib.error, urllib.parse
def getUrlContent(url):
#Downloads and returns content
return requests.get(url).content
def downloadUrl(url):
#Downloads and returns content
response = urllib.request.urlopen(url)
webContent = response.read()
return webContent
def SaveFile(filename,data):
file = open(filename,"w")
file.write(data)
file.flush()
file.close()
def SaveBinaryFile(filename,data):
file=open(filename,"wb")
file.write(data)
file.flush()
file.close()
def SaveImageFromUrlToFile(imageurl,filename):
data=getUrlContent(imageurl)
SaveBinaryFile(filename,data)

analyzer.py


from bs4 import BeautifulSoup as bs
import downloader as dd
def GetDivs(html):
scraper = bs(html, 'html.parser')
divs=scraper.find_all("div")
divisions={}
n=0
for div in divs:
divisions[n]=divs[n]
n=n+1
return divisions
def GetHeadings(html):
scraper = bs(html, 'html.parser')
h1s=scraper.find_all("h1")
headings={}
n=0
for h1 in h1s:
headings[n]=h1s[n]
n=n+1
return headings

#scraper.title.string
# scraper.div.string

 

 

Analyzer Use


import downloader as dd
import analyzer as an
from flask import Flask,render_template
app = Flask(__name__)
#d={2:"Mumbai",1:"Jaipur",3:"Varanasi"}

data=dd.downloadUrl("http://varanasikshetra.com")
d=an.GetDivs(data)
for heading in d.values():
print(heading.text)
@app.route("/")

def index():
return render_template('display.html',dict=d)

if __name__ == "__main__":
app.run(host="localhost", port=int("777"))

templates/dislay.html


<!DOCTYPE html>

<html>
<head>
<title>Products</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>
<body>

<div>
<center>

</center>
<table border="1">
<tr><td>Key</td><td>Value</td></tr>
{% for key in dict.keys()%}

{% set value=dict.get(key)%}

<tr><td>{{key}}</td><td>{{value.text}}</td></tr>

{% endfor %}
</table>
</div>
</body>
</html>

end

Leave a Reply

Your email address will not be published. Required fields are marked *