2017-12-07 20 views
0

私は複数のページを取得するWebスクレイピングプログラムを持っていますが、whileループを数値に設定する必要があります。最後のページに到達したらループを停止する条件を作りたい、または掻き取る項目がもうないことを認識したい。私はいくつのページが存在するか分からないと仮定します。どのように乱数を入れずにwhileループ条件を停止するように変更するのですか?私はreplace(","," ")を使用する必要はありませんので、私はデータを保存するためにモジュールcsvを使用してもデータBSを使ったPython Web掻き取り

data = soup.select('.result-info') 
    if not data: 
     print('END: no data:') 
     break 

が存在しない場合に終了し、無限ループとbreakを実行するためにwhile Trueを使用

import requests 
from bs4 import BeautifulSoup 
import csv 

filename="output.csv" 
f=open(filename, 'w', newline="",encoding='utf-8') 
headers="Date, Location, Title, Price\n" 
f.write(headers) 

i=0 
while i<5000: 
    if i==0: 
     page_link="https://portland.craigslist.org/search/sss?query=xbox&sort=date" 
    else: 
     page_link="https://portland.craigslist.org/search/sss?s={}&query=xbox&sort=date".format(i) 
    res=requests.get(page_link) 
    soup=BeautifulSoup(res.text,'html.parser') 
    for container in soup.select('.result-info'): 
     date=container.select('.result-date')[0].text 
     try: 
      location=container.select('.result-hood')[0].text 
     except: 
      try: 
       location=container.select('.nearby')[0].text 
      except: 
       location='NULL' 
     title=container.select('.result-title')[0].text 
     try: 
      price=container.select('.result-price')[0].text 
     except: 
      price="NULL" 
     print(date,location,title,price) 
     f.write(date+','+location.replace(","," ")+','+title.replace(","," ")+','+price+'\n') 
    i+=120 
f.close() 
+0

こんにちは、質問の質問部分を含めるのを忘れてしまったようです。現在存在するのは問題の説明だけです。答えがあるように質問を更新してください。 –

+1

'while while'を使用し、さらにページを読むことができないときに 'break'を使います(' try/except') – furas

+0

私はこれを試して、ブレークを認識しませんでした。掻き取るアイテムがなくなりました。 – Tarzan

答えて

1


テキストに,がある場合は、" "にテキストを挿入します。

s={}は、?の後の任意の場所に入れることができるので、最後にコードを読みやすくしてください。 (:私のコードでは、より読みやすい名前offsetを持ってBTW)

完全なコードを

ポータルは、あなたがs=0を使用する場合でも、私はi == 0
を確認する必要はありません。最初のページを提供します。

import requests 
from bs4 import BeautifulSoup 
import csv 

filename = "output.csv" 

f = open(filename, 'w', newline="", encoding='utf-8') 

csvwriter = csv.writer(f) 

csvwriter.writerow(["Date", "Location", "Title", "Price"]) 

offset = 0 

while True: 
    print('offset:', offset) 

    url = "https://portland.craigslist.org/search/sss?query=xbox&sort=date&s={}".format(offset) 

    response = requests.get(url) 
    if response.status_code != 200: 
     print('END: request status:', response.status) 
     break 

    soup = BeautifulSoup(response.text, 'html.parser') 

    data = soup.select('.result-info') 
    if not data: 
     print('END: no data:') 
     break 

    for container in data: 
     date = container.select('.result-date')[0].text 

     try: 
      location = container.select('.result-hood')[0].text 
     except: 
      try: 
       location = container.select('.nearby')[0].text 
      except: 
       location = 'NULL' 
     #location = location.replace(","," ") # don't need it with `csvwriter` 

     title = container.select('.result-title')[0].text 

     try: 
      price = container.select('.result-price')[0].text 
     except: 
      price = "NULL" 
     #title.replace(",", " ") # don't need it with `csvwriter` 

     print(date, location, title, price) 

     csvwriter.writerow([date, location, title, price]) 

    offset += 120 

f.close() 
+0

シンプルでエレガント! – Tarzan

関連する問題