【问题标题】:Request the second load of the page with Python使用 Python 请求页面的第二次加载
【发布时间】:2016-03-26 20:27:43
【问题描述】:

我很高兴使用 Python requestsBeautifulSoup 从 www.century21.com 抓取房产数据。网站有分页,我可以删除第一页的结果,但是当我尝试对第二页做同样的事情时,我得到了第一页的数据作为输出。

这是首页结果的示例:http://www.century21.com/real-estate/ada-oh/LCOHADA/#t=0&s=0

以下是同一搜索词的第二页的结果:http://www.century21.com/real-estate/ada-oh/LCOHADA/#t=0&s=10

我注意到,当我手动单击第二个 URL 以在浏览器中打开它时,第一个 URL 的结果会显示几秒钟,然后页面似乎已完全加载并显示第二个页面的结果。

您可以想象,Python request 正在抓取第二页的第一次加载结果,这恰好与第一页的结果相同。如果我请求第三页结果,第四页,依此类推。

下面是我的代码。如果你运行它,它会打印两次第一页的第一个属性的地址。

知道如何获取正确的页面结果吗?

from bs4 import BeautifulSoup
import requests

page1=requests.get("http://www.century21.com/real-estate/ada-oh/LCOHADA/#t=0&s=0")
c1=page1.content
soup1=BeautifulSoup(c1,"html.parser").find_all("div",{"class":"propertyRow"})[0].find_all("span",{"class":"propAddressCollapse"})[0].text

page2=requests.get("http://www.century21.com/real-estate/ada-oh/LCOHADA/#t=0&s=10")
c2=page2.content
soup2=BeautifulSoup(c2,"html.parser").find_all("div",{"class":"propertyRow"})[0].find_all("span",{"class":"propAddressCollapse"})[0].text

print(soup1)
print(soup2)

【问题讨论】:

    标签: python beautifulsoup python-requests


    【解决方案1】:

    向“search.c21”端点发出请求,从“list”键中获取 HTML 字符串并解析它:

    from bs4 import BeautifulSoup
    import requests
    
    page1 = requests.get("http://www.century21.com/search.c21?lid=COHADA&t=0&s=0&subView=searchView.AllSubView")
    c1 = page1.json()["list"]
    soup1 = BeautifulSoup(c1, "html.parser").find_all("div", {"class": "propertyRow"})[0].find_all("span", {
        "class": "propAddressCollapse"})[0].text
    
    page2 = requests.get("http://www.century21.com/search.c21?lid=COHADA&t=0&s=10&subView=searchView.AllSubView")
    c2 = page2.json()["list"]
    soup2 = BeautifulSoup(c2, "html.parser").find_all("div", {"class": "propertyRow"})[0].find_all("span", {
        "class": "propAddressCollapse"})[0].text
    
    print(soup1)
    print(soup2)
    

    打印:

    5489 Sr 235
    202 W Highland Ave
    

    【讨论】:

    • 非常感谢!只是一个问题:你是怎么想出那个 URL 的?有什么逻辑吗?
    • @A.S 当然,我使用浏览器开发工具并检查了“网络”选项卡,观察在页面加载期间发送了哪些请求..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    相关资源
    最近更新 更多