如何将python打印结果输出为表格或CSV格式?

时间:2016-04-20 11:21:04

标签: python csv web-scraping beautifulsoup

我是python的新手,现在我正在尝试为个人旅行计划学习网页抓取。打印结果后,我想知道如何将其输出为表格格式或CSV格式。

现在结果如下:

{'price': '115', 'name': 'The hotel name'}
{'price': '97', 'name': 'the hotel name'}
.......

我用Google搜索了一些模块的方法,比如pandas和prettytable,发现这对我来说太难理解了。所以我在这里看看是否有任何关于我的问题的解决方案。

代码如下:

import requests                   
from bs4 import BeautifulSoup

url="http://hotelname.com/arrivalDate=05%2F23%2F2016**&departureDate=05%2F24%2F2016" #means arrive on May23 and leaves on May 
wb_data = requests.get(url)
soup = BeautifulSoup(wb_data.text,'lxml')
names = soup.select('.PropertyName')
prices = soup.select('.RateSection ')
for name,price in zip(names,prices):    
    data = {
           "name":name.get_text(),
           "price":price.get_text()
            }
    print (data)`

3 个答案:

答案 0 :(得分:0)

我希望你以前的代码是正确的和写作部分;

import csv
with open('filename.csv', 'w') as csvfile:    #create csv file with w mode
    fieldnames = ['names', 'prices']          #header names
    writer = csv.DictWriter(csvfile, fieldnames=fiel dnames)    #set the writer
    writer.writeheader()    
    for name,price in zip(names,prices):     #writing the elements 
        writer.writerow({'names': name.get_text(), 'prices': price.get_text()})

答案 1 :(得分:0)

您可以使用pandas创建DataFrame,然后将DataFrame写入csv。 这需要稍微更改你的dict以使其更容易,代码看起来像这样:

import pandas as pd

#...

data = {                                        
    0: {"name": name.get_text()},
    1: {"price": price.get_text()}
}

df = pd.DataFrame.from_dict(data, orient='index')
df.to_csv('filename.csv', index=False)

答案 2 :(得分:0)

您只需要写入要打印值的文件:

import requests
import csv

from bs4 import BeautifulSoup

url="http://hotelname.com/arrivalDate=05%2F23%2F2016**&departureDate=05%2F24%2F2016" #means arrive on May23 and leaves on May 
wb_data = requests.get(url)
soup = BeautifulSoup(wb_data.text,'lxml')
names = soup.select('.PropertyName')
prices = soup.select('.RateSection ')

with open('results.csv', 'w') as outfile:
    writer = csv.writer(outfile, delimiter=',')
    writer.writerow(['Name', 'Price']]

    for name,price in zip(names,prices):
        writer.writerow([name.get_text(), price.get_text()])