0
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,但它沒有工作網絡報廢后將數據導出到csv文件
我試圖與具有開放( 「data.csv」, 「W +」)作爲csvfile: 作家= csv.writer(csvfile) writer.writerow([ 「SrNo」, 「名稱」]) writer.writerow([「Data 1」,「Data 2」]) –