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 파일로 내보내기
난 csvfile로 ("+ W", "data.csv")와 함께 개방 시도 writer.writerow ([ "데이터 1", "데이터 2"]) –