How to scrape only specific rows based on filter instead getting all rows with BeautifulSoup?

Question:

I am trying to scrape this site "https://takipcimerkezi.net/services" and for example as a starter I wanted to scrape ‘minimum siparis’ column.

This is my code:

from bs4 import BeautifulSoup
import requests

url='https://takipcimerkezi.net/services'
page= requests.get(url)
table=BeautifulSoup(page.content, 'html.parser')

min_sipariş= table.find_all(attrs={"data-label":"Minimum Sipariş"})

for i in min_sipariş:
    value=i.text
    print(f'Minimum Sipariş={value}')

When i try to scrape it im getting this kind of output:

Minimum Sipariş=100 
Minimum Sipariş=50 
Minimum Sipariş=10000 
Minimum Sipariş=10 
Minimum Sipariş=1000 
Minimum Sipariş=10 
Minimum Sipariş=20 
Minimum Sipariş=50
Minimum Sipariş=10
Minimum Sipariş=20 
Minimum Sipariş=100 
Minimum Sipariş=10 

My problem is, in the real site and HTML code 6th row actually an another entry and it’s ‘minimum siparis’ = 20. But in beautifulsoup there are another entry which is not found in the inspect code and not in the site’s entries.

But somehow beautifulsoup getting more code from the inspect HTML code how can I solve it and scrape the data as in the exact order in the site?

EDIT:

There is a line such as this when I print table and look the code i n my text editor in vscode:

</tr>
<tr data-filter-table-category-id="1">
<td data-filter-table-service-id="1387" data-label="ID">1387</td>
<td class="table-service" data-filter-table-service-name="true" data-label="Servis">4034-❤️ S4 - Instagram Türk Beğeni | Max 15K | %80 Türk    | Hızlı Başlar</td>
<td data-label="1000 adet fiyatı ">
                                                      13.01 TL
                                                  </td>
<td data-label="Minimum Sipariş">10</td>
<td data-label="Maksimum Sipariş">8000</td>
<td class="services-list__description" data-label="">
<div class="component_button_view">
<div class="">
<button class="btn btn-actions btn-view-service-description" data-content-id="#service-description-id-2-1387" data-max="8000" data-min="10" data-service-id="1387" data-service-name="4034-❤️ S4 - Instagram Türk Beğeni | Max 15K | %80 Türk    | Hızlı Başlar" data-target="#service-description-2" data-toggle="modal">
                                    Açıklama
                                  </button>
</div>
</div>
<div class="d-none" id="service-description-id-2-1387">
                                ℹ️ Kategori Yöneticisinden Özel Bilgi<br/> ️ Son Bilgi Tarihi: 3.1.2023 - 10:30<br/> ️ Servis saatte ortalama 2-3K gönderim yapmakta en hızlı beğeni servislerinden biridir. Datası Türk profil fotoğraflı nadiren fotoğrafsız hesaplardır. <br/><br/>  Min: 10<br/>  Max: 5.000<br/>  Link: Paylaşım Linki<br/>  Kalite: %100 Türk<br/>  Beğeni sayısı gizli gönderilere işlem sağlar.<br/>  Hız: Anlık Başlar<br/><br/>Notlar: <br/>  Servis yoğun olduğu zamanlarda işleme başlama hızı değişmektedir. <br/>  Verdiğiniz sipariş sistemde tamamlanmadan aynı linke 2. siparişi vermeyiniz.
                              </div>
</td>
</tr>

but this lines does not exist in the source code.

Asked By: luthierz

||

Answers:

Due to the limited information on the concrete settings of the OP, it is difficult to define a precise answer, so this is only intended to point in one direction and clarify the specifics.


If filter settings are made after the page call, this will only have an effect on the result in your browser. requests only knows the initial data, everything that happens afterwards in terms of rendering, filtering or anything else has no influence on this data, so you have to take the filtering of the data into account independently.

Issue#1: Select your elements based on entries that have a corresponding relation to the filter, e.g. via the data-filter-table-category-id:

soup.select('#service-tbody [data-filter-table-category-id="1473"]:has([data-label="Minimum Sipariş"])')

Issue#2: To get the values in euro you have to send your cookies with the request, cause the information about currency is saved there:

cookies={'user_currency':'27d210f1c3ff7fe5d18b5b41f9b8bb351dd29922d175e2a144af68924e3064d1a%3A2%3A%7Bi%3A0%3Bs%3A13%3A%22user_currency%22%3Bi%3A1%3Bs%3A3%3A%22EUR%22%3B%7D;'}

Example

from bs4 import BeautifulSoup
import requests

url='https://takipcimerkezi.net/services'

soup = BeautifulSoup(
        requests.get(
            url,
            cookies={'user_currency':'27d210f1c3ff7fe5d18b5b41f9b8bb351dd29922d175e2a144af68924e3064d1a%3A2%3A%7Bi%3A0%3Bs%3A13%3A%22user_currency%22%3Bi%3A1%3Bs%3A3%3A%22EUR%22%3B%7D;'}
        ).text
       )

data =  [
            dict(zip(e.find_previous('thead').stripped_strings,e.stripped_strings))
            for e in soup.select('#service-tbody [data-filter-table-category-id="1473"]:has([data-label="Minimum Sipariş"])')
        ]

Output of data

[{'ID': '1537',
  'Servis': '4755-  Instagram Türk Takipçi   | Max 2K | %100 Gerçek Kullanıcılar | Düşme Olmaz |xa0Hızlı Başlar',
  '1000 adet fiyatı': '≈ 25.1955 €',
  'Minimum Sipariş': '5',
  'Maksimum Sipariş': '3000',
  'Açıklama': 'Açıklama'},
 {'ID': '1538',
  'Servis': '4759-  Instagram Global Takipçi   | Max 3K | %100 Gerçek Kullanıcılar | Düşme Olmaz | Hızlı Başlar',
  '1000 adet fiyatı': '≈ 8.2339 €',
  'Minimum Sipariş': '50',
  'Maksimum Sipariş': '3000',
  'Açıklama': 'Açıklama'},
 {'ID': '1539',
  'Servis': '4761- ━━━━━━━━━━━━━ Beğeni ━━━━━━━━━━━━━',
  '1000 adet fiyatı': '≈ 4990.1436 €',
  'Minimum Sipariş': '99999',
  'Maksimum Sipariş': '99999'},
 {'ID': '1540',
  'Servis': '4757-❤️ Instagram Türk Beğeni   | Max 1K | %100 Gerçek Kullanıcılar | Düşme Olmaz | Hızlı Başlar',
  '1000 adet fiyatı': '≈ 12.2834 €',
  'Minimum Sipariş': '10',
  'Maksimum Sipariş': '1000',
  'Açıklama': 'Açıklama'},
 {'ID': '1541',
  'Servis': '4758-❤️ Instagram Türk Kadın Beğeni     | Max 500 | %100 Gerçek Kullanıcılar | Düşme Olmaz | Hızlı Başlar',
  '1000 adet fiyatı': '≈ 13.6477 €',
  'Minimum Sipariş': '5',
  'Maksimum Sipariş': '500',
  'Açıklama': 'Açıklama'},
 {'ID': '1542',
  'Servis': '4760-❤️ Instagram Global Beğeni   | Max 2K | %100 Gerçek Kullanıcılar | Düşme Olmaz | Hızlı Başlar',
  '1000 adet fiyatı': '≈ 3.6609 €',
  'Minimum Sipariş': '20',
  'Maksimum Sipariş': '3000',
  'Açıklama': 'Açıklama'},
 {'ID': '1543',
  'Servis': '4756-  Instagram Türk Yorum   | Max 200 | %100 Gerçek Kullanıcılar | Düşme Olmaz | Hızlı Başlar',
  '1000 adet fiyatı': '≈ 28.3453 €',
  'Minimum Sipariş': '5',
  'Maksimum Sipariş': '500',
  'Açıklama': 'Açıklama'}]
Answered By: HedgeHog