【问题标题】:Parsing Yelp using lxml - ignore html tag使用 lxml 解析 Yelp - 忽略 html 标签
【发布时间】:2014-12-22 02:33:49
【问题描述】:

我正在尝试运行以下代码来提取 Yelp 评论

from lxml import html  
import requests  
import csv  
page = requests.get('http://www.yelp.com/biz/guisados-los-angeles')

review = tree.xpath('//p[@itemprop="description"]/text()')

现在,我有如下评论

这些炸玉米饼就是生意。 我们冒险进入了一家朴实无华、相对较小的餐厅,那里提供了他们所拥有的不同炸玉米饼的照片菜单(非常有帮助)。

以上评论单条评论正在拆分为以下列表

[
    'These tacos are the business.', 
    'We ventured into an unpretentious, relatively small restaurant who offered a photographic menu (VERY helpful) of the different tacos they have.
]

如何让 lxml text() 忽略评论中的 <br>?请大家指点一下?

【问题讨论】:

    标签: python web-scraping lxml yelp lxml.html


    【解决方案1】:

    据我了解,您希望每个评论文本都作为一个字符串。

    使用itemprop="description" 遍历p 元素并获得.text_content():

    for review in tree.xpath('//p[@itemprop="description"]'):
        print review.text_content()  # alternatively: ' '.join(review.xpath('text()'))
        print "----"
    

    打印:

    These tacos are the business.We ventured into an unpretentious, relatively small restaurant who offered a photographic menu (VERY helpful) of the different tacos they have.  The friendliest of cashiers and servers greeted us.  My group and I each got the sampler with additional pescado and camarones tacos, and a quesadilla.  We pretty much ordered the whole menu and they were patient as we picked out the individual tacos for our samplers.  To echo what others have said, the corn tortillas are BOMB.com, but the braised meats also hold their own.  The whole experience is one amazing party in your mouth.  Their horchata is also a must-order.The habanero salsa that they have (I forget what they call it) really is a thing of beauty.  The spice kicks you on the tongue well after the salsa has slid down your throat.  If you're a chili eater like me, you need to get an extra side of this!  It won't let you down.  Also, they serve Stumptown coffee!  A+
    ----
    We've been meaning to pay this place a visit for over a year. Now I wonder why we waited so long? This isn't your "traditional" taqueria, so if you're craving street taco style food, go to Tacos Gavilan. If you want a trip down memory lane with the bursting flavors and spices of old-school guisos in the form of tacos, then this is the spot. My partner and I each had a sampler, each trying a different variation of the plate.All of the options are delicious, the only one that left me feeling just a bit disappointed was the tacos de calabasitas, it was ok, but it just felt a bit bland. If you like spicy, try the tinga. The cochinita pibil also comes in a very spicy sauce, but for the sampler they use a very mild sauce. My personal favorite was the Hongos con Cilantro. Yes, it's meatless and I'm addicted. Pure perfection, bursting with flavor. To drink, we ordered an Horchata and  an Armando Palmero. The latter is their version of an Arnold Palmer, mixing Jamaica with Lemonade, it was good and refreshing, best as a summer drink. However, I'm hooked on their horchata, this is the real deal, none of that nasty powdered fake horchata. This horchata is sweetened just right and you can taste and feel the graininess of the toasted rice, pure oldschool deliciousness. Best Horchata in LA! Overall, we plan on returning here and recommend you try it at least once.
    ----
    ...
    

    请注意,评论文本中没有保留空格和换行符。这是您可以解决的问题(如果需要),请参阅:

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 2011-12-16
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      相关资源
      最近更新 更多