2017-09-29 4 views
1

J'ai écrit un code qui peut rayer quelques détails d'une page Web. Ma question est chaque fois que je cours mon code, il imprime la sortie comme:Comment incrémenter des données dans chaque cellule en csv et quelques questions [python]

| ['Kapil Sarawagi' '[email protected]' '1412702594'] | | ['MA ARCHICTECTS PRIVATE LIMITED' '[email protected]' '1414299999'] | | ['Prabhu Dayal Kanojiya' '[email protected]' '9829055412'] |

Cependant, je veux que ce soit comme.

| ['Kapil Sarawagi' '[email protected]' '1412702594'] |

| ['MA ARCHICTECTS PRIVATE LIMITED' '[email protected]' '1414299999'] |

| ['Prabhu Dayal Kanojiya' '[email protected]' '9829055412'] |

comme dans chaque cellule ... comment puis-je le faire?

deuxième question, comment puis-je rendre mon code professionnel? mon style de codage est-il mauvais? Et comment puis-je le rendre plus court ci-dessous est mon 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]) 

Répondre

1

Un code plus court pour atteindre le même objectif:

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] 

Résultats:

['Prabhu Dayal Kanojiya', '[email protected]', '9829055412'] 
['MA ARCHICTECTS PRIVATE LIMITED', '[email protected]', '1414299999'] 
['Kapil Sarawagi', '[email protected]', '1412702594'] 
+0

Hey! c'est très bon codage .. merci beaucoup ... mon style de codage est-il pauvre? Si oui, comment puis-je l'améliorer? –

+0

@ Mr.Bones c'est ok. Continuez simplement à pratiquer, en lisant du code, et vous améliorerez certainement vos compétences de codage. –