【发布时间】:2017-11-21 21:55:01
【问题描述】:
任何帮助将不胜感激,因为我是 python 新手。我创建了下面的网络爬虫,但它不会爬取所有页面,只有 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)
【问题讨论】:
-
是否出现错误或是否干净退出?
page变量是 18 还是 2?
标签: python web-scraping beautifulsoup web-crawler