【问题标题】:I am getting stuck in a loop with my webscraper我的 webscraper 陷入了一个循环
【发布时间】:2020-09-26 22:39:17
【问题描述】:

我正在制作一个网页抓取工具,当我尝试抓取一页数据时,它会不断加载相同的信息。

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup 

my_url = 'https://www.realtor.com/realestateagents/phoenix_az'

#opening up connection, grabbing the page
uClient = uReq(my_url)
#read page 
page_html = uClient.read()
#close page
uClient.close()

#html parsing
page_soup = soup(page_html, "html.parser")

#finds all realtors on page 
containers = page_soup.findAll("div",{"class":"agent-list-card clearfix"})

for container in containers:
    name = page_soup.find('div', class_='agent-name text-bold')
    agent_name = name.text.strip()

    number = page_soup.find('div', class_='agent-phone hidden-xs hidden-xxs')
    agent_number = number.text.strip()

    print("name: " + agent_name)
    print("number: " + agent_number)

【问题讨论】:

  • “它不断加载相同的信息”是什么意思?如果你抓取同一个页面,它为什么要加载不同的信息?
  • 因为您搜索的是page_soup,而不是container
  • 你做过调试吗?请参阅How to Askhelp center

标签: python python-3.x web-scraping


【解决方案1】:

解决方案是在循环内搜索container 而不是page_soup

此外,您应该检查是否有结果或捕获抛出的异常。

【讨论】:

    猜你喜欢
    • 2021-04-16
    • 2015-11-20
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多