我在试着分析这个站点-https://www.rent-a-car-crete.gr/car-search/ 为了获得必要的信息,解析器必须输入以下数据

 data = {
    'pickup_location': '29',
'return_to_pickup_location': '1',
'return_location': '---',
'pickup': '21.02.2024',
'pickup_time': '10:00',
'return': '22.02.2024',
'return_time': '10:00',
'promo_code': '',
'age': '30+',
'pickup_id': '',
'return_id': '',
'subimt_search': '1',
    }

(查看了网络上的完整榜单) 以及如何按下搜索按钮并解析出来的汽车

I tried to parse the main page - https://www.rent-a-car-crete.gr enter image description here without a post request I received it, when sending a post request I also received it, I was not transferred to the page with the car, after looking at what I get when parsing the main page and the page described above, I realized that I get the same text everywhere, I can’t understand how I can achieve the result I need. Here are screenshots that can help and my code [enter image description here] (https://i.stack.imgur.com/Ahv3c.png) enter image description here

url = 'https://www.rent-a-car-crete.gr/car-search/'
urlpre = 'https://www.rent-a-car-crete.gr'

work =  Session()

res = work.post(url=url, headers=headers,data=data)
soup = BeautifulSoup(res.content, 'html.parser')
cars = soup.find_all('h3', class_='carname')
print(res)
print('-'*50)
print(soup)
print('-'*50)
print(cars)

推荐答案

发布搜索参数的URL略有不同(您应该在Web开发人员工具->;网络选项卡中看到它):

import requests
from bs4 import BeautifulSoup

api_url = "https://www.rent-a-car-crete.gr/CarRental/load.php"
params = {"controller": "GzFront", "action": "getCars", "local": "3"}

data = {
    "local": "3",
    "pickup_location": "29",
    "return_to_pickup_location": "1",
    "return_location": "---",
    "pickup": "22.02.2024",
    "pickup_time": "10:00",
    "return": "23.02.2024",
    "return_time": "10:00",
    "promo_code": "",
    "age": "30+",
    "continue": "Select",
    "pickup_id": "",
    "return_id": "",
    "subimt_search": "1",
}

soup = BeautifulSoup(
    requests.post(api_url, params=params, data=data).content, "html.parser"
)

for h3 in soup.select("h3.carname"):
    print(f'{h3.text:<60} {h3.find_next(class_="price").text.strip()}')

打印:

Toyota Aygo or similar                                       € 79 
Peugeot 108 or similar                                       € 79 
Suzuki Alto or similar                                       € 79 
Volkswagen Up! or similar                                    € 79 
Skoda Citigo Aut or similar                                  € 93 
Fiat 500 Hybrid                                              € 86 
Fiat Panda or similar                                        € 80 

...

Python相关问答推荐

大Pandas 胚胎中产生组合

Mistral模型为不同的输入文本生成相同的嵌入

根据二元组列表在pandas中创建新列

Telethon加入私有频道

使用groupby方法移除公共子字符串

字符串合并语法在哪里记录

使用Python从rotowire中抓取MLB每日阵容

ConversationalRetrivalChain引发键错误

替换现有列名中的字符,而不创建新列

通过追加列表以极向聚合

在二维NumPy数组中,如何 Select 内部数组的第一个和第二个元素?这可以通过索引来实现吗?

如何在Python中使用Iscolc迭代器实现观察者模式?

如何将一组组合框重置回无 Select tkinter?

如何根据rame中的列值分别分组值

如何在Gekko中处理跨矢量优化

如何将返回引用的函数与pybind11绑定?

如何在Python中解析特定的文本,这些文本包含了同一行中的所有内容,

有了Gekko,可以创建子模型或将模型合并在一起吗?

如何在Polars中创建条件增量列?

Pandas查找给定时间戳之前的最后一个值