私はpython 3でサイトの4つの異なるページからデータをスクラップするためのスクリプトを作成しました。それは正常に動作しますが、私はCSVファイルでその結果を取得しようとすると、何かがうまくいかず、最後のページの情報しか印刷しません。誰かが私にこれを助けてくれましたか?私はあなたの検討のためにスクリプトを添付しました。私が間違っていることを知りたくない。Pythonでの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)
ファイルを開くと、おそらく既存のファイルが消去されます。 docu – frlan