【问题标题】:Python>bs4 Scraping website based on choice from dropdown listPython>bs4 根据下拉列表中的选择抓取网站
【发布时间】:2021-01-12 19:01:05
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: python web-scraping beautifulsoup python-requests


    【解决方案1】:

    这里需要请求带有数据的发布请求。从开发网络分流器收集数据。

    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
    

    希望对你有所帮助。

    【讨论】:

    • 据我所知,第一,我会从下拉列表中收集值 > 第二,我会按照你给我看的格式设置它们“-data-example”">第三我会用“post”方法发出请求得到我想要的??
    • 是的,你可以关注它stackoverflow.com/questions/8550114/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多