Issue
I have written a code that that can scrape few details from a webpage. My question is whenever I run my code it prints the output like :
|['Kapil Sarawagi' '[email protected]' '1412702594']| |['MA ARCHICTECTS PRIVATE LIMITED' '[email protected]' '1414299999']| |['Prabhu Dayal Kanojiya' '[email protected]' '9829055412']|
however I want it to be like:
|['Kapil Sarawagi' '[email protected]' '1412702594']|
|['MA ARCHICTECTS PRIVATE LIMITED' '[email protected]' '1414299999']|
|['Prabhu Dayal Kanojiya' '[email protected]' '9829055412']|
like in each cell...how can I do this?
Second question, how can I make my code look for professional? Is my coding style bad? And how can I make it shorter?
This is my code:
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen
import csv
url = "http://www.rera-rajasthan.in/Home/ViewProject?id=JgMAAA"
html = urlopen(url)
soup = BeautifulSoup(html, "html.parser")
finaldata = []
data = soup.find_all("div", {"class":"panel-body"})
#filename = "Rajasthan.csv"
#f = open(filename, "r")
for i in data:# to get engineer
date = i.find_all("table", {"class":"table table-bordered"})
getname = date[21].find_all("td")
name = getname[1].text
email = getname[0].text
phone = getname[3].text
sublist = []
fname = [name, email, phone]
sublist.append(fname)
for i in data:# to extract architect
date = i.find_all("table", {"class":"table table-bordered"})
getname = date[20].find_all("td")
name = getname[1].text
email = getname[0].text
phone = getname[3].text
#sublist = []
fname = [name, email, phone]
sublist.append(fname)
for i in data:# to extract contractor
date = i.find_all("table", {"class":"table table-bordered"})
getname = date[19].find_all("td")
name = getname[1].text
email = getname[0].text
phone = getname[3].text
#sublist = []
fname = [name, email, phone]
sublist.append(fname)
finaldata.append(sublist)
with open("output.csv", "w")as csvfile:
writer = csv.writer(csvfile, delimiter=',',quotechar='|', lineterminator='\n')
for i in range(0, len(finaldata)):
writer.writerow(finaldata[i])
Solution
A shorter code to achieve the same goal:
import requests
from lxml import html
response = requests.get('http://www.rera-rajasthan.in/Home/ViewProject?id=JgMAAA')
tree = html.fromstring(response.content)
# Getting al <h3> tags with 'TableHeading' class
for heading in tree.xpath('//h3[@class="TableHeading"]'):
# Extracting <h3> heading name/text
heading_name = heading.xpath('text()')[0]
# Checking if <h3> heading name has one of these names
# We only want to get data from the table next to each one of them
if heading_name in ['CONTRACTOR', 'ARCHITECT', 'STRUCTURAL ENGINEER']:
# As each table heading has a table below (following-sibling) them
# We extract the data from that only table (table[1])
email, name, address, phone = heading.xpath('.//following-sibling::table[1]//tr/td/text()')
print [name, email, phone]
Results:
['Prabhu Dayal Kanojiya', '[email protected]', '9829055412']
['MA ARCHICTECTS PRIVATE LIMITED', '[email protected]', '1414299999']
['Kapil Sarawagi', '[email protected]', '1412702594']
Answered By - Andrés Pérez-Albela H.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.