在ajax站点中查找网页编号以进行网页抓取

2024-09-28 01:25:16 发布

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

我想用python和BeautifulSoup来抓取一个站点,但是我找不到页码,我只能抓取站点的第一页,我认为这个站点使用了Ajax,当我更改页面时,URL地址不会改变

这是网站的链接:

https://ihome.ir/sell-residential-apartment/th-tehran

这是我的代码,我想<>强>刮擦< /强>这个站点的20页,用价格、基础等细节来刮房子

import requests
from bs4 import BeautifulSoup

response = requests.get("https://ihome.ir/sell-residential-apartment/th-tehran")


soup = BeautifulSoup(response.json(), "html.parser")
prices = soup.select('.sell-value')
titles = soup.select('.title')

homes_prices = []
for home in prices:
    homes_prices.append(int(''.join(filter(str.isdigit, home.getText()))))


homes_titles = []
for title in titles:
    homes_titles.append(title.getText())

res = dict(zip(homes_titles, homes_prices))

for key, value in res.items():
    p = str(res[key])
    if len(str(res[key])) <= 2:
        p += '000000000'
    if len(str(res[key])) > 2:
        p += '000000'

    print(key.strip(), int(p))

Tags: keyinhttpsfor站点titleresprices
1条回答
网友
1楼 · 发布于 2024-09-28 01:25:16

没有必要使用BeautifulSoup作为您正在寻找的data。已在JSON目录中显示

这里是Back-EndAPI,从中获取数据

当您查看scrape{}页以及包含24项的每一页时

所以它是24 * 20=480,所以我将每页的结果调整为480,并调用API一次,比在页面上循环多次要好

现在你有了一个JSON目录,你可以访问和提取你想要的任何东西

import requests


params = {
    'is_sale': '1',
    'source': 'website',
    'paginate': '480',
    'page': '1',
    'locations[]': 'iran.th.tehran',
    'property_type[]': 'residential-apartment'
}


def main(url):
    r = requests.get(url, params=params).json()
    for item in r['data']:
        print(item.keys())


main("https://scorpion.ihome.ir/v1/flatted-properties")

相关问题 更多 >

    热门问题