【发布时间】:2021-02-05 00:58:47
【问题描述】:
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 试过,但没有用
【问题讨论】:
-
我尝试使用 open("data.csv", "w+") as csvfile: writer = csv.writer(csvfile) writer.writerow(["SrNo", "Name"]) writer .writerow(["数据 1", "数据 2"])
标签: python-3.x csv