首页 > 解决方案 > BeautifulSoup 给我列表空

问题描述

无论出于何种原因,我似乎无法找到一种方法来打印 div 或 span 元素之间的文本。我想创建一个包含一些评论的列表。

import request
from bs4 import BeautifulSoup
r=request.get('https://www.yelp.ie/biz/tesla-san-francisco?osq=Tesla=Dealership')
r.status_code
r.text
soup = BeautifulSoup(r.text, 'html.parser')
divs = soup.findaAll(class_="lemon--div__373c0__1mboc margin-b2__373c0__abANL border-color--default__373c0__3-ifU")
reviews = []
for div in divs:
reviews.append(span.find('p').text,'\n')

结果是[]

标签: pythonbeautifulsoup

解决方案


您需要使用selenium,因为内容似乎是动态生成的。

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
driver.get('https://www.yelp.ie/biz/tesla-san-francisco?osq=Tesla=Dealership')
sleep(5)
divs = driver.find_element_by_xpath("//*[@id=\"wrap\"]/div[3]/div/div[3]/div/div/div[2]/div/div/div[1]/div/div[1]/div[3]/section[2]/div[2]")
comments = divs.find_elements_by_tag_name('p')
reviews = []
for comment in comments:
    reviews.append(comment.text)

for review in reviews:
    print(review)

输出:-

Your trust is our top concern, so businesses can't pay to alter or remove their reviews. Learn more.



Today was delivery day and we were pretty excited to collect our car. When we arrived we proceeded upstairs where the magic happens.

Jessica H. and Alex were both helping other customers and we were third in line. Even though we waited less than 15 minutes we were told they'd be with us promptly and they thanked us for our patience and for waiting. They made us feel valued and the time passed quickly. When it was our turn, Jessica explained the process, walked us through the documents she'd prepared in advance of our arrival (and even colour coded to make everything both easy and efficient), and explained to us what to expect.

The place was spotless. We saw the staff wearing masks, cleaning surfaces and office supplies after every use and they even had separate cups for clean versus dirty pens. These folks have this down to a science.

Our car was ready for us and Alex gave us a mini-tutorial, asked if we needed help and was prepared to answer questions even though we had none. He addressed one of our concerns and as we finished up, Jessica stepped-in to help get us on our way.

Jessica and Alex were really very helpful and kind. We were struck with their efficiency, how quickly they worked together to move between customers and how gracious and empathetic they were with us and everyone we saw them interact with during our visit. Five star service for sure.

A special note: Jessica H. obviously has a talent for service. She was attentive, involved and compassionate with all the customers she assisted today. She should be commended for the finesse with which she juggles customers and to-dos.

We loved the experience today and it elevated the brand in our eyes and affirmed the reason we chose to own a Tesla

点评继续……


推荐阅读