簡體   English   中英

python美麗的湯輸出到excel

[英]python beautiful soup output into excel

我正在嘗試將python腳本的輸出轉換為excel。 該腳本在Python中工作正常,但是當我嘗試執行導入CSV和writerow規則時,該腳本不起作用。 它說價格在寫行中沒有定義,我將如何打印多個項目。 任何幫助,將不勝感激。

import csv
import requests
from bs4 import BeautifulSoup

f = open('dataoutput.csv','w', newline = "")
writer = csv.writer(f)

def trade_spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'http://www.zoopla.co.uk/for-sale/property/manchester/?identifier=manchester&q=manchester&search_source=home&radius=0&pn=' + str(page)
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)
        for link in soup.findAll('a', {'class': 'listing-results-price text-price'}):
            href = "http://www.zoopla.co.uk" + link.get('href')
            title = link.string 
            get_single_item_data(href) 
        page +=1

def get_single_item_data(item_url): 
    source_code = requests.get(item_url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text)
    for item_name in soup.findAll('div', {'class': 'listing-details-address'}):
     address = item_name.string
     print(item_name.get_text(strip=True))
    for item_fame in soup.findAll('div', {'class' : 'listing-details-price text-price'}):
        price = item_fame.string 
        print(item_fame.get_text(strip=True))

writer.writerow(price)

trade_spider(1)

在腳本中,函數get_single_item_data之外的任何地方都沒有定義對象price 在該功能之外,您的代碼無法識別具有該名稱的任何對象。 另外, get_single_item_data不會從BeautifulSoup對象返回任何內容。 它只打印它。 您應該將函數重寫為如下形式:

def get_single_item_data(item_url): 
    source_code = requests.get(item_url)
    plain_text = source_code.text
    soup = BeautifulSoup(plain_text)

    #create list to contain addresses
    addresses = []

    for item_name in soup.findAll('div', {'class': 'listing-details-address'}):
        address = item_name.string

        #add each address to the list
        addresses.append(address)

        print(item_name.get_text(strip=True))

    #create list for prices
    prices = []

    for item_fame in soup.findAll('div', {'class' : 'listing-details-price text-price'}):
        price = item_fame.string 

        #add prices to list
        prices.append(price)

        print(item_fame.get_text(strip=True))

  #alter the code to return the data structure you prefer.
  return([addresses,prices])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM