在网络报废后将数据导出到csv文件

时间:2017-03-12 21:26:03

标签: python-3.x csv

import requests 
from bs4 import BeautifulSoup
base_url = "https://www.yelp.com/search?find_desc=&find_loc="
loc = "Newport+Beach,+CA"
page = 10
url = base_url + loc + '&start='+ str(page)
yelp_r = requests.get(url)
yelp_soup = BeautifulSoup(yelp_r.text, 'html.parser')
businesses = yelp_soup.findAll('div',{'class':'biz-listing-large'})
file_path = 'yelp-{loc}.txt'.format(loc=loc)

with open(file_path,"a") as textfile:
    businesses = yelp_soup.findAll('div',{'class':'biz-listing-large'})
    for biz in businesses:
        title = biz.findAll('a',{'class':'biz-name'})[0].text
        print(title)
        address = biz.findAll('address')[0].text
        print(address)
        phone= biz.findAll('span',{'class':'biz-phone'})[0].text
        print(phone)
        page_line="{title}\n{address}\{phone}".format(
            title=title,
            address=address,
            phone=phone
        )
       textfile.write(page_line)

如何将数据导出到csv文件,现在它被导出到txt文件。我尝试过使用csv.writer,但它没有工作

1 个答案:

答案 0 :(得分:0)

我不是Python的专家,但如果我是你,这基本上就是我的意思。

a,a,a,a,a...