gpt4 book ai didi

python - 使用Python请求页面的第二次加载

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:41 25 4
gpt4 key购买 nike

我很高兴使用 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)

最佳答案

向“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

关于python - 使用Python请求页面的第二次加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36234999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com