Python -如何使用 selenium 刮Yelp评论?

wvmv3b1j  于 2023-02-10  发布在  Python
关注(0)|答案(2)|浏览(82)

我正在开发一个Python应用程序,它可以帮助我获得特定餐厅的评论。我正在使用Selenium 4. 1 Web Scraper与Python。
在我的项目文件夹中设置Selenium驱动程序后,我根据Selenium文档将以下代码放在一起:

#YELP REVIEW SCRAPER                                 #

#Importing Dependencies
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# Setting up driver options
options = webdriver.ChromeOptions()
# Setting up Path to chromedriver executable file
CHROMEDRIVER_PATH ='../Selenium/chromedriver.exe'
# Adding options
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
# Setting up chrome service
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
# Establishing Chrom web driver using set services and options
driver = webdriver.Chrome(service=service, options=options)

driver.get('https://www.yelp.com/biz/taste-of-texas-houston')

这成功地打开了Yelp页面的餐厅,我想得到评论,但当我试图刮的评论使用:

driver.find_element(By.CLASS_NAME, ' raw__09f24__T4Ezm')

其中:“raw__09f24__T4Ezm”是第一次评审的span类的名称,我得到错误:

InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
  (Session info: chrome=96.0.4664.45)
Stacktrace:
Backtrace:
    Ordinal0 [0x00BD6903+2517251]
    Ordinal0 [0x00B6F8E1+2095329]
    Ordinal0 [0x00A72848+1058888]
    Ordinal0 [0x00A74F44+1068868]
    Ordinal0 [0x00A74E0E+1068558]
    Ordinal0 [0x00A75070+1069168]
    Ordinal0 [0x00A9D1C2+1233346]
    Ordinal0 [0x00A9D63B+1234491]
    Ordinal0 [0x00AC7812+1406994]
    Ordinal0 [0x00AB650A+1336586]
    Ordinal0 [0x00AC5BBF+1399743]
    Ordinal0 [0x00AB639B+1336219]
    Ordinal0 [0x00A927A7+1189799]
    Ordinal0 [0x00A93609+1193481]
    GetHandleVerifier [0x00D65904+1577972]
    GetHandleVerifier [0x00E10B97+2279047]
    GetHandleVerifier [0x00C66D09+534521]
    GetHandleVerifier [0x00C65DB9+530601]
    Ordinal0 [0x00B74FF9+2117625]
    Ordinal0 [0x00B798A8+2136232]
    Ordinal0 [0x00B799E2+2136546]
    Ordinal0 [0x00B83541+2176321]
    BaseThreadInitThunk [0x757C6739+25]
    RtlGetFullPathName_UEx [0x773B8AFF+1215]
    RtlGetFullPathName_UEx [0x773B8ACD+1165]

我试着研究这个错误,但没有运气。有没有什么想法如何修改我的代码,这样我就可以得到这个特定餐厅的所有可用评论,这样我就可以得到评论的日期,人,分数和评论的文本?

vm0i2vca

vm0i2vca1#

driver.get('https://www.yelp.com/biz/taste-of-texas-houston')
reviews=driver.find_elements_by_xpath("//ul[contains(@class,'undefined')]//li[contains(@class,'margin-b5')]//div[contains(@class,'review')]")
values=[[review.find_element_by_xpath(".//div[contains(@class,'user-passport')]//a").text,
         review.find_element_by_xpath(".//div[contains(@class,'i-stars')]").get_attribute("aria-label"),
         review.find_element_by_xpath(".//div[contains(@class,'margin-t1')]//div[2]").text,
         review.find_element_by_xpath(".//span[contains(@class,'raw')]").text.replace("\n"," ").replace("\\'","'")
         ] for review in reviews]
print(values)

无法获取最新的评论来为评论部分提取正确的值,但可以这样做。找到所有要获取的评论,然后对每个值进行xpath。
输出:

[['Melissa G.', '5 star rating', '10/19/2021', 'This restaurant is my favorite restaurant of all time! I\'m so glad they were able to operate during COVID! I have loved this restaurant for years and have neglected to write a review for them. Shame on me...I know!!!  We go here for special occasions and not because it\'s overly expensive (because it\'s not), but because this place is special! Read the different sections in their "About Us" on their website.  Here we go: The Restaurant: Whether the holidays are happening or it\'s just a regular business day, the restaurant is beautiful and well maintained. You won\'t see trash in the parking lot here. You also won\'t see food droppings on the floor. They take extra care to give you a top notch experience here.  The Staff: They always smile and treat you like royalty. The people who work here have encouragement. School tuition paid for! From what I heard, there is a wait list to work here. Anyhow, you are greeted with smiles, seated with smiles and served with smiles. You will not ever have a reason to complain about the staff and the service you receive.  The Food: There is not a single thing they serve that is bad (This is what I have heard from those who have ordered other things on the menu). The salad bar is AMAZING! The Prime Rib...my ABSOLUTE favorite and the only thing I ever order! It is tender, delicious and not overly fatty. It cuts like butter! Don\'t be scared of the price. It is worth it and not overly priced.  Beverages: They make an excellent martini and have a pretty amazing selection of wine. The coffee is also excellent paired with your favorite dessert.  The Dessert: We had the Snickers Pie the first time I took my boyfriend here. It was complimentary for his birthday (and included a complimentary photo of us). It was AMAZING!!! They also shared the recipe!!! WHAT!!! No restaurant I know of has ever done that! We have also had the Slice of Heaven which was equally wonderful! Now, we have to order both when we go.  The Price: I do not find Taste of Texas to be overly expensive at all. Their prices are affordable enough that you can get an excellent meal with the salad bar included and service that exceeds service at any other restaurant.  How to Dress: I wouldn\'t go here wearing shorts, tennis shoes/flip flops and a t-shirt. You might feel a little out of place. I have only ever seen people in pants/slacks, nice shirts/tops, dresses and shoes to match.  Overall: I love this place and will forever continue to give them my business until the day I die!'], ['Shannon B.', '3 star rating', '9/22/2021', "Service: 5 stars Covid precautions: -5 no staff member wearing masks. The only precaution in place were gloves at the salad bar. This is an airborne sickness and u care about the salad bar the most? I feel bad for your staff members who are being exposed the most.  Food:  I came here expecting to be blown away due to the reviews. Maybe this was the issue, I had too high of an expectation. Ordered the cowboy steak and the lobster. The cut of meat was good quality and perfectly cooked. However, we had to add salt to our steak because it had no flavor. It was so bland that  I wanted to ask for A1 sauce but thought that would be rude.  My lobster tails were beautiful and large but were over cooked. Baked potato was perfectly cooked as well. Our favorite part was the salad bar. It's a high end salad bar with lots to choose from.  We also had the creme brulee which was good."], ['Jason Z.', '3 star rating', '9/20/2021', "This is a Houston staple in some regards. If you're traveling here and got a recommendation for a semi-touristy steakhouse but still at a good quality, there's a good chance you might go to Taste of Texas. Been here a couple times for lunch and dinner service. Honestly for me, it's an upscale version of a Saltgrass or Texas Roadhouse but for the price, I'd rather go for a Pappas or Vic & Anthony's. The steak quality here seems to have been going down over the years.  The steak here usually comes out cooked correctly, however I find them to be slightly underseasoned. The sides are alright, and overall everything is average or slightly above average. A lot of folks like the salad bar, I have not toured it in my previous visits. Solid 3.5/5"], ['Marty H.', '5 star rating', '12/2/2021\nUpdated review', 'Our favorite restaurant for family events. The food is always wonderful!! I highly recommend going there!!'], ['Jules R.', '5 star rating', '11/5/2021', 'We arrived to Christmas decorations galore on 11/2 and took me by surprise lol Loved that there was a salad bar and the mini loaf bread was so good. We got to pick our own steaks and they actually give you a number to your Steak so you get exactly what you chose.. it was a cool local Texan restaurant and I appreciate that they keep it old school'], ['Christine H.', '5 star rating', '8/16/2021', "Hands down, the best steak in Houston!  They also have an amazing salad bar filled with bricks of fresh cheese, warm buttery bread, and endless amounts of crisp salads and fruits.  Their service is impeccable. If it's your first time there, expect a warm and friendly southern welcome! The dress code is business casual.  There are no reservations, and the lines can be very long (first come, first serve). Sometimes, you'll be waiting for hours. However, they do provide free sodas, tortilla chips, salsa, and popcorn while you wait.  This is one of my favorite places to go to for Mother's Day and birthdays. On Mother's Day, they normally have a photo booth for the families. All the mothers receive a beautiful rose. If it's your birthday, they offer you a free dessert!"], ['Tanisha K.', '5 star rating', '7/17/2021', "Had a great experience here. One of the first things I noticed was that EVERYTHING was huge here. From the steaks you can purchase for home, the chandelier, the bear and the moose head. Maybe it was symbolic but I do agree everything is bigger in Texas.  The food: I ordered the salmon with no vinegarette and mustard but with the Oscar toppings. It came with garlic mashed potatoes and broccoli. Now FYI I asked for a loaded potato and he states they didn't have that but then I seen someone else order it and questioned why he would say that. I was a little disappointed for the misinformation but I understand that the way the dish is set up, it was better with no substitutions.  It also came with a salad bar but it was more than enough food that I had to pass. (It is a buffet salad bar, you make it yourself) My friend had the 10oz filet steak, Mac and Cheese with spinach. I only tried the spinach since my other reviews the Mac and cheese is hit or miss.. the sides were definitely bland and I don't like to add Salt after it's cooked. Overall, it was a nice place and I enjoyed the experience"], ['Steven T.', '2 star rating', '6/20/2021', 'Oh boy... I never thought I\'d be giving Taste of Texas (ToT) a bad review, but I think it\'s been a long time coming. We need to face the facts that the place isn\'t what it used to be. Even my visit last year was just "ok," like... 3-star ,"ok," but tonight was kind of disasterous.  5-6 years ago, I vividly remember designating this place to be my absolute favorite steakhouse--a place I could recommend to friends and family, regardless of its price point. But food hasn\'t quite been the same since. It\'s either that, or my own steaks at home have gotten so good, it just makes paying over $100 for a steak  just absolutely ludicrous. I will say the meat quality is great. It\'s a handsome piece of meat... but mishandled--a damn shame.  This is the first time I\'ve been denied a salad because our entire party didn\'t order their own individual steaks. The waitress stated that because we only ordered 2 steaks, only two of us can go to the salad bar. Out of curiosity, I asked how much it would cost for each person to go to the salad bar. "$18." Excuse me, WHAT? I just paid over $200 for 60 Oz of steak and you want us to pay another $60-ish for three more salads? Now don\'t get me wrong, the salad bar is very good, but not THAT good. Why would you expect anyone to eat anymore than 8-12 Oz of steak individually? Even in my prime (no pun intended), 32 Oz would be a daring challenge.  Sides are inconsistent with the amount served. Be wary that the "potatoes au gratin" ($7) will be served in a tiny dish smaller than the palm of your hand. Perhaps the restaurant\'s portion sizes have decreased in reflection to COVID (trying to reduce sharing/family style). That means if everyone wants a fair amount of potatoes, multiply that by $7 (e.g., party of 10 = $70). This just stinks of cash grab.  Apparently, ToT has raised their prices twice within the past few months with the most recent hike of 10% on all steaks added within a month ago. The place was already considered pretty expensive to eat at, so experiencing a drop in quality along with price gouging is just plain unfair and leaves a terrible after taste. They gave us extra large share plates, one that was clearly dirty. We informed them and they swapped it--but again, I digress that this is unacceptable at this price point.  I welcome some elements (I guess this will be my usual recommendation section). They successfully (but barely) made our reservation time. I think the soda bar outside definitely elevates the experience (even though half of the syrups were out). I\'m glad the place doesn\'t require you to valet (which comes as a complete surprise considering their price point). I think you\'ll be burning money by ordering a tomahawk steak (medium-rare); as they have poor temperature control--at least a third of my steak was chewy-purple. Admittedly, all sides we\'ve ordered were delicious (Mac, Brussel sprouts, potatoes, crab cake). Quail and jalapeño poppers were on point.  When it comes to food, I\'m not a cheapskate. I HEAVILY emphasize that the bang needs to match the buck, and right now, Taste of Texas is highway robbery. Go anywhere else. You\'ll find a better and complimentary salad bar at any Brazilian steakhouse and better tasting meats (along with variety and quality) and, surprisingly, at half the cost.  In the past, people have told me that this place is a tourist trap and now I\'m finally convinced that it is. My father-in-law said to cut them some slack because they\'re probably tired from making so many steaks. I disagree. [At this price point] They should be able to reproduce quality steaks blindfolded, if not in their sleep. UNACCEPTABLE.'], ['Dan D.', '5 star rating', '12/4/2021', 'Excellent steaks and service! Great decor for the holidays and the salad bar is best in Houston.'], ['Ashley Y.', '5 star rating', '9/21/2021', 'This place is so cool and the food is amazing. The service is exquisite. You get to go to the salad bar where they have a variety of salads to choose from, they have a variety of breads and spreads you can choose from. When it comes to steaks, these guys are the best! You order your steak and you can go in the back in the butcher shop and choose which steak you want. Sides are also included. The steaks ended up being amazing. This is one of the best steak joints in houston hands down!']]
omqzjyyz

omqzjyyz2#

使用此代码来获得星星餐厅在yelp网站

reviews=driver.find_element(By.XPATH,"/html/body/yelp-react-root/div[1]/div[3]/div[1]/div[1]/div/div/div[2]/div[1]/span/div")
aria_label = reviews.get_attribute("aria-label")
print(aria_label)

首先找到可以进行星星评定的元素,然后找到属性“aria-label”,最后将“aria-label”的值存储到变量中

相关问题