Невозможно записать данные в файл Excel (несколько вкладок) с помощью Python - PullRequest
0 голосов
/ 26 октября 2018

Я не очень хорошо знаком с записью данных в формате Excel с использованием Python, мне нужна помощь, чтобы записать вывод моих данных в один файл .xlsx (Excel) с несколькими вкладками.

Мой код приведен здесь:

import time
import requests
import random
from lxml import html 
from bs4 import BeautifulSoup
import xlsxwriter

def write_to_file(file, mode, data, newline=None, with_tab=None):  
    with open(file, mode, encoding='utf-8') as l:
        if with_tab == True:
            data = '\t'.join(data)
        if newline == True:
            data = data+'\n'
        l.write(data)

link = ["http://ec.europa.eu/environment/ets/ohaDetails.do?returnURL=&languageCode=en&accountID=&registryCode=&buttonAction=all&action=&account.registryCode=&accountType=&identifierInReg=&accountHolder=&primaryAuthRep=&installationIdentifier=&installationName=&accountStatus=&permitIdentifier=&complianceStatus=&mainActivityType=-1&searchType=oha&resultList.currentPageNumber="+str(var)+"&nextList=Next%C2%A0%3E&selectedPeriods=" for var in range(17500)] # This will read the URL's line by line as per specific value of var.
start = 1 
end = 20 

for pagenum, links in enumerate(link[start:end]):
    print(links)
    r = requests.get(links)
    time.sleep(random.randint(2,5))
    soup = BeautifulSoup(r.content,"lxml")

# Table 2
    for items in soup.find(id="tblAccountContactInfo").find_all("tr")[:]:
        dataset = [item.get_text(strip=True) for item in items.find_all("td")[:]]
        print(dataset)

        write_to_file('Table3.tsv', 'a', dataset, with_tab=True, newline=True)
        write_to_file('Table3.tsv', 'a', links)

# Table 3
    for items in soup.find(id="tblChildDetails").find("table").find_all("tr"):

        dataset = [item.get_text(strip=True) for item in items.find_all("td")[:]]
        print(dataset)

        write_to_file('Table3.tsv', 'a', dataset, with_tab=True, newline=True)
        write_to_file('Table3.tsv', 'a', links)

        #workbook = xlsxwriter.Workbook('Table3.xlsx')
        #worksheet = workbook.add_worksheet("Table 3")
        #worksheet.write(dataset)
        #workbook.close()

Мне нужен вывод в листе .xlsx Excel на нескольких вкладках, таких как вкладка Таблица 1 и вкладка Таблица 2, в настоящее время я получаю данные в формате .tsv.Я пробовал xlsxwriter, но не смог получить результаты, поэтому прокомментировал эти строки.Пожалуйста, помогите

1 Ответ

0 голосов
/ 28 октября 2018

Сначала необходимо создать две таблицы и отслеживать текущую строку, которая будет использоваться для каждой таблицы.Затем функция append_row() может добавить одну строку данных на требуемый лист.

import time
import requests
import random
from lxml import html 
from bs4 import BeautifulSoup
import xlsxwriter


def append_row(ws, row):
    for col, value in enumerate(row):
        ws.write_string(ws.cur_row, col, value)

    ws.cur_row += 1


workbook = xlsxwriter.Workbook('output.xlsx')
ws_2 = workbook.add_worksheet("Table 2")
ws_3 = workbook.add_worksheet("Table 3")

# Keep a track of the row to use in each worksheet
ws_2.cur_row = 0    
ws_3.cur_row = 0        

start = 1 
end = 3
link = "http://ec.europa.eu/environment/ets/ohaDetails.do?returnURL=&languageCode=en&accountID=&registryCode=&buttonAction=all&action=&account.registryCode=&accountType=&identifierInReg=&accountHolder=&primaryAuthRep=&installationIdentifier=&installationName=&accountStatus=&permitIdentifier=&complianceStatus=&mainActivityType=-1&searchType=oha&resultList.currentPageNumber={}&nextList=Next%C2%A0%3E&selectedPeriods="

for page_number in range(start, end):
    print("Page {}".format(page_number))
    url = link.format(page_number)
    r = requests.get(url)

    time.sleep(random.randint(2, 5))
    soup = BeautifulSoup(r.content, "lxml")

    # Table 2
    for items in soup.find(id="tblAccountContactInfo").find_all("tr")[:]:
        dataset = [item.get_text(strip=True) for item in items.find_all("td")[:]]
        append_row(ws_2, [url] + dataset])

    # Table 3
    for items in soup.find(id="tblChildDetails").find("table").find_all("tr"):
        dataset = [item.get_text(strip=True) for item in items.find_all("td")[:]]
        append_row(ws_3, [url] + dataset])

workbook.close()    
...