Python Forum
How to put data in every cell plus few questions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to put data in every cell plus few questions
#1
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

Output:
|['Kapil Sarawagi' '[email protected]' '1412702594']| |['MA ARCHICTECTS PRIVATE LIMITED' '[email protected]' '1414299999']| |['Prabhu Dayal Kanojiya' '[email protected]' '9829055412']| |['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 it?

second question, how can i make my code look for professional? is my coding style bad? and how can i make it shorter?below 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])
Reply
#2
Quote:second question, how can i make my code look for professional? is my coding style bad? and how can i make it shorter?below is my code:
Quote:for i in data:# to get engineer
dont use i, use a meaningful name

Quote:for i in range(0, len(finaldata)):
dont use range(len())

you can use next_sibling to get the next <td> instead of by index listing. You could do the same with each table as well. The way you have it is quite redundant.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  html data cell attribute issue delahug 5 3,140 May-31-2020, 09:18 AM
Last Post: delahug

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020