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

2024-05-28 19:19:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我很高兴从www.century21.com网站使用Python requestsBeautifulSoup。站点中有分页功能,我可以丢弃第一页的结果,但是当我尝试对第二页做同样的操作时,我得到了第一页的数据作为输出。在

以下是第一页结果的示例: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的结果会显示几秒钟,然后页面似乎完全加载并显示第二个页面的结果。在

可以想象,Pythonrequest正在获取第二个页面第一次加载的结果,这恰好与第一个页面的结果相同。同样,如果我要求第三页的结果,第四页,等等。在

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

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

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)

Tags: comhttpwww页面allfindrequestsreal
1条回答
网友
1楼 · 发布于 2024-05-28 19:19:12

请求“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)

印刷品:

^{pr2}$

相关问题 更多 >

    热门问题