繁体   English   中英

无法使用BeautifulSoup Python从按钮获取文本

[英]Cannot get text from button using BeautifulSoup Python

我有一个页面,我想从一个按钮获取文本,我认为这是由js控制的

<button class="shop-tab js-shop-tab icon location-tab single-shop "><span>Location</span></button>

我试试

location = soup.select('location-tab').text

location = soup.find('button', attrs={'class':'shop-tab js-shop-tab icon location-tab single-shop'}).text

就像我用硒一样

location = post.find_element_by_css_selector(".location-tab").text

但我得到NoneType我怎么能用Beautifulsoup得到它? 谢谢

根据主要问题的评论,要获得销售该产品的所有商店的信息,您可以使用:

import requests
from bs4 import BeautifulSoup
html = requests.get("https://www.skroutz.gr/s/2195774/Western-Digital-Blue-3-5-1TB-7200rpm.html").text
soup=BeautifulSoup(html, 'html.parser')
lis = soup.findAll("li", {"class": "card js-product-card"})
for li in lis:
    shop_id = li["data-shopid"]
    prod_id = li["data-product-id"]
    # get shop inforamtion
    si = requests.get(f"https://skroutz.gr/m/summary/{shop_id}/{prod_id}").json()
    # si now contains a clean json object with all the shop info, phone, full_address, coordinates, etc.

{'shop': {'id': 3964, 'name': 'Mdstore', 'url': '/m/3964/Mdstore', 'logo_url': '//c.scdn.gr/ds/shops/logos/3964/mid_20170103102940_411b7fcf.jpeg', 'latest_reviews_count': 40, 'new_review_url': '/account/shops/reviews/new?shop_id=3964', 'review_stats_per_impression': [{'impression': 'positive', 'impression_text': 'Θετικές', 'percentage': 85, 'count': 34}, {'impression': 'negative', 'impression_text': 'Αρνητικές', 'percentage': 15, 'count': 6}], 'locations': [{'id': 5271, 'lat': '38.056695', 'lng': '23.806215', 'info': '', 'feature_description': 'Κατάστημα & Σημείο παραλαβής.', 'phone2': '2106141179', 'pickup_point': True, 'store': True, 'visible_on_product_card': True, 'phone': '2106141118', 'full_address': 'Διονύσου 3, Τ.Κ. 15124, Μαρούσι, Αττική', 'distance': 10.4, 'distance_tier': 3, 'address': {'city': 'Μαρούσι', 'region': 'Αττική', 'street': 'Διονύσου 3', 'postcode': 'Τ.Κ. 15124', 'full': 'Διονύσου 3, Τ.Κ. 15124, Μαρούσι, Αττική', 'structured': True}}], 'paypal': False, 'phone': '2106141179', 'reviews_label': 'αξιολογήσεις', 'is_reseller': False, 'official_reseller_text': '', 'formatted_latest_review_score': '4,3', 'rating_percent': 86, 'details_icons': [], 'has_pickup_points': True, 'time_on_platform_in_words': ['2', 'χρόνια'], 'orders_to_range': '50+', 'top_reasons': ['Έγκαιρη παράδοση', 'Φιλική εξυπηρέτηση', 'Προσεγμένη συσκευασία προϊόντων'], 'closest_store_details': None}}
{'shop': {'id': 2749, 'name': 'Multicopter', 'url': '/m/2749/Multicopter', 'logo_url': '//c.scdn.gr/ds/shops/logos/2749/mid_fba77f19-03b8-421c-bdcd-8853740ab740.png', 'latest_reviews_count': 407, 'new_review_url': '/account/shops/reviews/new?shop_id=2749', 'review_stats_per_impression': [{'impression': 'positive', 'impression_text': 'Θετικές', 'percentage': 99, 'count': 401}, {'impression': 'negative', 'impression_text': 'Αρνητικές', 'percentage': 1, 'count': 6}], 'locations': [{'id': 2760, 'lat': '38.016931', 'lng': '23.649653', 'info': '', 'feature_description': 'Κατάστημα & Σημείο παραλαβής.', 'phone2': '', 'pickup_point': True, 'store': True, 'visible_on_product_card': True, 'phone': '2110125139', 'full_address': 'Αφροδίτης 9, Τ.Κ. 12462, Χαϊδάρι, Αττική', 'distance': 7.7, 'distance_tier': 2, 'address': {'city': 'Χαϊδάρι', 'region': 'Αττική', 'street': 'Αφροδίτης 9', 'postcode': 'Τ.Κ. 12462', 'full': 'Αφροδίτης 9, Τ.Κ. 12462, Χαϊδάρι, Αττική', 'structured': True}}], 'paypal': False, 'phone': '2110125139', 'reviews_label': 'αξιολογήσεις', 'is_reseller': False, 'official_reseller_text': '', 'formatted_latest_review_score': '4,7', 'rating_percent': 94, 'details_icons': [], 'has_pickup_points': True, 'time_on_platform_in_words': ['3+', 'χρόνια'], 'orders_to_range': '100+', 'top_reasons': ['Έγκαιρη παράδοση', 'Φιλική εξυπηρέτηση', 'Ενημέρωση για την εξέλιξη της παραγγελίας'], 'closest_store_details': None}}
...

注意:审核您的请求或谷歌重新验证码会让您感到烦恼。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM