python美丽的汤输出到excel

时间:2017-06-03 17:01:20

标签: python excel csv beautifulsoup

我试图将python脚本的输出转换为excel。该脚本在Python中运行良好,但是当我尝试执行导入CSV和编写器规则时它不起作用。它说在编写器中没有定义价格,我将如何打印多个项目。任何帮助,将不胜感激。

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)

1 个答案:

答案 0 :(得分:0)

对象price未在函数get_single_item_data之外的脚本中的任何位置定义。在该函数之外,您的代码无法识别具有该名称的任何对象。此外,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])