遍历 Web 元素

时间:2021-05-03 01:35:11

标签: python selenium selenium-webdriver

我正在尝试从 Yelp 中抓取日期。我注意到评论日期的 web 元素只有一个不同,它是我输入 num 变量的数字。我希望将数字分配给 num 变量,然后增加变量的值,直到没有其他东西可以抓取。我仍然是一个 python 菜鸟,希望得到一位老专家的帮助?

代码在下面,我的想法是将元素添加到数组中,然后在循环之前增加它,这样它就不会立即匹配并中断循环。

这是网页 https://www.yelp.com/biz/gordon-ramsay-hells-kitchen-las-vegas-3?osq=Restaurants&sort_by=date_desc

为了澄清起见,这是我感兴趣的审核日期。

    def date_harvesting(self):
    self.num = []
    num = 1
    i = 0
    tester = self.driver.find_element_by_xpath('/html/body/div[2]/div[3]/yelp-react-root/div/div[3]/div/div/div[2]/div/div[1]/div[2]/section[2]/div[2]/div/ul/li[' + str(num) + ']/div/div[2]/div/div[2]/span')
    self.num.append(tester.text)
    num += 1
    while i < num:
        if self.num[i] != tester.text:
            self.num.append(tester.text)
            i += 1
            num += 1
        else:
            i += 1
    print(tester)
    print(self.num)

1 个答案:

答案 0 :(得分:1)

您最初创建数组的想法是正确的。 但是,就我而言,这种情况下的 while 循环增加了更多的复杂性。 如果您只想获取所有元素,请使用 for 循环。 我已经测试了以下示例:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get('https://www.yelp.com/biz/gordon-ramsay-hells-kitchen-las-vegas-3?osq=Restaurants&sort_by=date_desc')
wait = WebDriverWait(driver, 15)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".review__373c0__13kpL.border-color--default__373c0__2oFDT")))
cards = driver.find_elements_by_css_selector(".review__373c0__13kpL.border-color--default__373c0__2oFDT")
reviews = []
for card in cards:
    date = card.find_element_by_css_selector(".margin-t1__373c0__oLmO6.margin-b1-5__373c0__2Wblx.border-color--default__373c0__2oFDT").text
    feedback = card.find_element_by_css_selector(".margin-b2__373c0__abANL.border-color--default__373c0__2oFDT>p>span").text
    reviews.append([date, feedback])
for i in reviews:
    print(i)

结果是反馈和审查日期的列表:

['5/2/2021', "Hell's Kitchen definitely lived up to its reputation! Every single dish was exceptionally cooked and perfectly seasoned. While the hostess was quite snotty and rude upon check in until she realized we DID have a reservation, this unacceptable welcome was overshadowed by exquisite food and a wonderful, attentive waiter who made great recommendations and enhanced our dining experience tenfold. Already making plans to dine here again during our next trip to Vegas.\n\nFor appetizers I had:\n* Lobster Risotto (5/5!! And I dislike the texture of risotto)\n* Pan Seared Scallops (4/5 but not a huge scallop fan)\n\nMain courses and sides I had:\n* Beef Wellington (5/5)\n* Braised Short Rib (5/5)\n* Filet Mignon (I didn't try this)\n* Baked Mac & Cheese (5/5)\n* Potato purée (5/5 so delicious and creamy)\n* Roasted asparagus (5/5)\n\nDesserts I had:\n* sticky toffee pudding  (5/5 highly recommend)\n* salted peanut decadence (5/5 I love peanuts so this was my favorite)\n* chocolate sin cake (4/5 delicious, a bit too rich for my personal taste)\n* coconut 3-ways (5/5 this was the most surprising at how delicious it was, and definitely the favorite of the others at the table)"]
['5/2/2021', "A must dine experience when in Vegas!\n\nWe've been to Gordon Ramsey's steak and although we thought the food there was excellent, this places beats it by a whole notch. The beef Wellington was flaky, crispy on the outside and soft and tender in the middle-- I mean, how do they do it?! Also, they seemed to have gotten their welly down much better than at GRSteak.\n\nScallops were the most perfectly cooked scallops. Salmon was fresh and perfectly crispy. I mean everything was Divinely delicious from start to finish!\n\nService was AMAZING!\n\nMake reservations now. We couldn't get in last time and made reservations 6 weeks out.\n\nSo goood, and so worth the $$$$\n\nDefinite highlight of our trip!!"]
['5/1/2021', 'Food was good, but for the prices of everything i expected a lot more from here. I was a little disappointed but it was okay. Staff were great though! Everyone was very nice and pleasant!']
['5/1/2021', "This is a good spot for a nice dinner, and I would say you at least have to go once while you're in Vegas. We did have a bad experience with the reservation because they cancelled it without notifying us. When we showed up to the restaurant, the staff was very rude, and they told us we would just have to make a new reservation for a different day. Luckily, we still had a few days left in Vegas, and we were able to reserve a table for the next evening. When we arrived, the staff at the front was still rude, but our waiter was excellent! He recommended what dishes we should try, and they were very good! The food is definitely very unique. However, Gordon Ramsay's famous dish, the Beef Wellington, was pretty average. The rest of the food was phenomenal though! The side dishes were served family style. Also, the drink called, Notes from Gordon, is amazing, and I would for sure recommend! Overall, it's worth it to at least go to the restaurant once for the experience and unique food!"]
['5/1/2021', "First impression, very hard to make a reservation, then we arrived ontime just to be told to wait 15-20 min for a table. Host did not introduce herself just sent us to our table, didn't ask what seating we would prefer. Had to ask to be seated by the window.\n\nFood is overpriced, we ordered the 3 course dinner $80 but its beef Wellington ONLY, hopefully we can have an option for Prime Rib or Tomahawk Ribeye. The beef Wellington was tender but didn't have that WOW factor, Cesar Salad was plain they didnt even offer parmesan or cracked pepper. Desert bread pudding was the best part of the meal but  I felt like we just ate at Black Agus restaurant with poor service. This is basically a 3 star restaurant with over priced food. Gordon Ramsey i had high hopes in your establishments"]
['4/30/2021', "Not very impressed.  Have been to Gordon Ramsay Steak twice and thought it was expensive, but definitely worth the money.  I thought this place was just very expensive and the food was in no way worth it.  And I have been to a lot of the nice places in town including Michael's at the South Point, The Steakhouse at Circus Circus, Hanks at Green Valley, just to name a few."]
['4/30/2021', "We came for lunch to celebrate mine and my dad's birthdays. I had the fixed lunch with the beef Wellington with Caesar's salad and the banana pudding. The cocktail called A Note from Gordon was very tasty.  The beer Wellington was cooked perfectly and the salad was a healthy portion. The service was impeccable. Definitely worth the money and the waiter was extremely friendly."]
['4/29/2021', 'Wow the servers are amazing! Great drinks and good presentation. Place really holds up . We heard all the hype but this place seems for real ! A must try !']
['4/29/2021', "It's Gordon so you expected it to be great! And it is. Basic but delicious and vegan friendly!! Yay!"]
['4/29/2021', "Hell's Kitchen was, in a word, fantastic! They are located OUTSIDE of  Caesar's Palace on the Las Vegas Strip.\n\nFirst off, they have replicated the show to an almost exact replica. The chefs are even wearing team colors-- red or blue!\n\nThe do require a reservation for tables, however we were able to secure a couple seats at the bar for full service.\n\nI had a three course meal package. I started with seared sea scallops, pea purée, and a reduction. For my main course, I ordered the Beef Wellington with potato mash, root vegetables and a red wine reduction. For dessert, we had the Sticky Toffee pudding.\n\nEverything was so flavorful and cooked to perfection! Definitely worth the high price tag! Check them out the next time you come to Vegas."]