파이썬 3에서 사이트의 4 개의 다른 페이지에서 데이터를 긁어내는 스크립트를 만들었습니다. 그것은 잘 작동하지만 내가 CSV 파일에 그 결과를 얻으려고하면, 뭔가 잘못되고 그것은 단지 마지막 페이지의 정보를 인쇄합니다. 아무도 나를 도울 수 없어요. 내가 고려할 스크립트를 첨부했습니다. 내가 뭘 잘못하고 있는지 알고 싶어하지 마.파이썬으로 CSV 조작 3
import csv
import requests
from bs4 import BeautifulSoup
def web_crawler(mpage):
page=1
while page<=mpage:
url=requests.get("http://www.yellowpages.com/search?search_terms=Coffee%20Shops&geo_location_terms=San%20Francisco%2C%20CA&page="+str(page))
soup=BeautifulSoup(url.text,'html.parser')
x=soup.findAll(class_='info')
gist=[]
for z in x:
Item=z.findAll(class_="business-name")
for Title in Item:
Name=Title.text
Patta=z.findAll(class_="adr")
for Thikana in Patta:
Address=Thikana.text
Number=z.findAll(class_="phones")
for Token in Number:
Phone=Token.text
metco=(Name,Address,Phone)
print(metco)
gist.append(metco)
outfile=open('data.csv','w',newline='')
writer=csv.writer(outfile)
writer.writerow(["Name","Address","Phone"])
writer.writerows(gist)
page+=1
web_crawler(4)
w를 사용하여 파일을 열면 기존 파일이 지워질 수 있습니다. docu – frlan