Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
993 views
in Technique[技术] by (71.8m points)

Python>bs4 Scraping website based on choice from dropdown list

as an example, i have a super-market website and there s a section showing market's stores. for choosing locations a dropdown list exists. What i am trying to do is based on my choice from dropdown list i want to get quantities of stores (blue frame). here s picture;

enter image description here

I accomplished getting values from dropdown list by this code:

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

after that, I am stucked. what I want is telling the computer to select the city (from citys list) from dropdown and then getting number (blue frame)

I'd be grateful if you tell me the path that i need to take.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is need to requests post request with data. Data collect from the dev network tap.

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

Hopes it help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...