Python>bs4 抓取网站基于从下拉列表中的选择

时间:2021-01-12 19:01:05

标签: python web-scraping beautifulsoup python-requests

例如,我有一个超市网站,其中有一个部分显示了市场的商店。用于选择位置的下拉列表存在。 我想要做的是基于我从下拉列表中的选择我想获得商店的数量(蓝框)。 这是图片;

enter image description here

我通过这段代码完成了从下拉列表中获取值:

import requests
from bs4 import BeautifulSoup

url="https://www.migros.com.tr/en-yakin-migros"

r=requests.get(url)
ht=r.content
soup=BeautifulSoup(ht,"html.parser")


soup= soup.find("div",class_="stores-selection-container stores-city-select address-part")
items=soup.select("option[value]")

#values=[item.get("value") for item in items]

cities=[item.text for item in items]
del cities[0] #first index is empty and removed

在那之后,我被卡住了。我想要的是告诉计算机从下拉列表中选择城市(从城市列表中),然后获取数字(蓝框)

如果你告诉我我需要走的路,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

这里需要使用数据来请求post请求。从开发网络分接头收集数据。

import requests
from bs4 import BeautifulSoup

# url="https://www.migros.com.tr/en-yakin-migros"

# r=requests.get(url)

data1 = {"cityName": "İSTANBUL",
"townName": "ADALAR",
"cityId": '', 
"townId": "34001"}
# 1 Mağaza Listeleniyor


# cityName: İSTANBUL
# townName: BAĞCILAR
# cityId: 34
# townId: 34025
    
data = {"cityName": "İSTANBUL",
"townName": "BAĞCILAR",
"cityId": '34', 
"townId": "34025"} 
# 11 Mağaza Listeleniyor

# cityName: İZMIR
# townName: ÇIĞLI
# cityId: 35
# townId: 35025

# 15 Mağaza Listeleniyor    

# cityName: İSTANBUL
# townName: ADALAR
# cityId: 
# townId: 34001

# cityName: İSTANBUL
# townName: ADALAR
# cityId: 
# townId: 34001

post_url = "https://www.migros.com.tr/stores"

response = requests.post(post_url, data=data)
print(response.status_code)
print()
soup=BeautifulSoup(response.content,"html.parser")
print(soup)

# soup= soup.find("div",class_="stores-selection-container stores-city-select address-part")
# items=soup.select("option[value]")
# items
# print('soup')

#values=[item.get("value") for item in items]

# cities=[item.text for item in items]
# cities
# del cities[0] #first index is empty and removed

希望对你有帮助。

相关问题