我有一个以下网页https://www.skroutz.gr/s/15013958/Hasbro-Monopoly-Classic-Game.html,我想从中获取每个卖方的价格,可用性,卖方名称。
它们作为div放在列表元素中,所以我做了以下
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome('C:\\chromedriver.exe')
browser.get("https://www.skroutz.gr/s/15013958/Hasbro-Monopoly-Classic-Game.html")
time.sleep(1)
elem = browser.find_element_by_tag_name("body")
no_of_pagedowns = 20
while no_of_pagedowns:
elem.send_keys(Keys.PAGE_DOWN)
time.sleep(0.2)
no_of_pagedowns-=1
post_elems = browser.find_elements_by_css_selector(".card.js-product-card")
for post in post_elems:
print(post.text)
到目前为止,我获得的数据是整个文本,而不是div。如何获得每条帖子的价格,可用性和卖家信息
我应该执行以下操作吗?还是还有其他正确的方法?
for post in post_elems:
prices = browser.find_elements_by_css_selector(".price")
sellers = browser.find_elements_by_css_selector(".shop-name")
availability = browser.find_elements_by_css_selector("p.availability")
答案 0 :(得分:1)
您可以使用post
元素来定位数据,它将发现仅属于特定帖子的数据
data = []
for post in post_elems:
price = post.find_element_by_css_selector(".price").text
seller = post.find_element_by_css_selector(".shop-name").text
availability = post.find_element_by_css_selector("p.availability").text
# add to list of lists
data.append([price, seller, availability])
# print
print(f'price: {price} seller: {seller} availability: {availability}')