2017-06-19 10 views
2

私はPythonを初めて使うので、助けてください。私は以下のWebクローラを作成しましたが、すべてのページをクロールするわけではなく、わずか2ページです。すべてのページをクロールするためには、どのような変更を加える必要がありますか?My BeautifulSoupスパイダーは全ページではなく2ページしかクロールしません

def trade_spider(max_pages)ループを参照してください。その下にはtrade_spider(18)があり、すべてのページをループする必要があります。

ありがとうございました。

import csv 
import re 
import requests 
from bs4 import BeautifulSoup 

f = open('dataoutput.csv','w', newline= "") 
writer = csv.writer(f) 

def trade_spider(max_pages): 
    page = 1 
    while page <= max_pages: 
     url = 'http://www.zoopla.co.uk/for-sale/property/nottingham/?price_max=200000&identifier=nottingham&q=Nottingham&search_source=home&radius=0&pn=' + str(page) + '&page_size=100' 
     source_code = requests.get(url) 
     plain_text = source_code.text 
     soup = BeautifulSoup(plain_text) 
     for link in soup.findAll('a', {'class': 'listing-results-price text-price'}): 
      href = "http://www.zoopla.co.uk" + link.get('href') 
      title = link.string 
      get_single_item_data(href) 
     page += 1 
def get_single_item_data(item_url): 
    source_code = requests.get(item_url) 
    plain_text = source_code.text 
    soup = BeautifulSoup(plain_text) 

    for item_name in soup.findAll('h2', {'itemprop': 'streetAddress'}): 
    address = item_name.get_text(strip=True) 
writer.writerow([address]) 
trade_spider(18) 
+0

エラーが発生しているか、正常に終了していますか? 'page'変数はそれを18にするのか、まさに2にするのでしょうか? –

答えて

0

コードは正常に動作していますが、すべてのページをクロールします(18ページでは14ページではありません)。この場合、2番目の関数は不要で、requests.get()を何度も呼び出すだけでクローラを遅くすることになります。私は少しコードを変更しましたが、これは速いです。

import csv 
import re 
import requests 
from bs4 import BeautifulSoup 

f = open('dataoutput.csv','w', newline="") 
writer = csv.writer(f) 

def trade_spider(max_pages): 
    page = 1 
    while page <= max_pages: 
     furl = 'http://www.zoopla.co.uk/for-sale/property/nottingham/?price_max=200000&identifier=nottingham&q=Nottingham&search_source=home&radius=0&pn=' + str(page) + '&page_size=100' 
     source_code = requests.get(url) 
     plain_text = source_code.text 
     soup = BeautifulSoup(plain_text) 

     # Changed the class' value 

     for link in soup.findAll('a', {'class': 'listing-results-address'}):  
      #href = "http://www.zoopla.co.uk" + link.get('href') 
      #title = link.string 
      #get_single_item_data(href) 
      address = link.get_text() 
      print (address)    # Just to check it is working fine. 
      writer.writerow([address]) 

     print (page) 
     page += 1 

# Unnecessary code 

'''def get_single_item_data(item_url): 
source_code = requests.get(item_url) 
plain_text = source_code.text 
soup = BeautifulSoup(plain_text) 

for item_name in soup.findAll('h2', {'itemprop': 'streetAddress'}): 
    address = item_name.get_text(strip=True) 
    writer.writerow([address])''' 

trade_spider(18) 
+0

Rajeevありがとう、上記のコードがアドレスを取得するように見えますが、私はそれが各リンクに入り、その情報を取得するのに必要なアドレスよりも多くの情報を必要とします。 trade_spider(14)を置くだけでも2ページの結果、何らかのアイデアが返されますか? – hello11

+0

私はコードを書き換え、すべてのページから情報を返しました。たぶんコードの他の部分(あなたが投稿していない可能性があります)は問題を引き起こしています – Rajeev

+0

ありがとうございましたRajeev、タイプエラーが発生していません。どのように過去のタイプを取得できますか? – hello11

関連する問題