首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python漂亮的汤输出到excel中

python漂亮的汤输出到excel中
EN

Stack Overflow用户
提问于 2017-06-04 01:01:20
回答 0查看 942关注 0票数 0

我正在尝试将python脚本的输出导入到excel中。该脚本在Python中运行良好,但当我尝试执行import CSV和writerow规则时,它不起作用。上面写着价格没有定义,以及如何打印多个项目。任何帮助都将不胜感激。

代码语言:javascript
运行
复制
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)
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44346471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档