【问题标题】:My BeautifulSoup spider only crawls 2 pages not all the pages我的 BeautifulSoup 蜘蛛只爬 2 个页面而不是所有页面
【发布时间】: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


【解决方案1】:

您的代码运行良好,它会抓取所有页面(尽管只有 14 个页面而不是 18 个页面)。似乎您试图抓取街道地址,在这种情况下,第二个函数是不必要的,并且只会通过调用 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)

【讨论】:

  • 感谢 Rajeev,看起来上面的代码将获取地址,但我想要的信息比需要它进入每个链接并获取该信息的地址更多。即使放置 trade_spider(14) 仍然只返回 2 页结果,有什么想法吗?
  • 我重新运行了您的代码,它从所有页面返回了信息。也许代码的其他部分(您可能尚未发布)导致问题
  • 感谢 Rajeev,发生了非类型错误。我如何通过 none 类型?
  • 如果没有看到代码,我无法帮助您。如果您对此没有任何问题,请将您的代码上传到某处并分享其链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多