python - 使用Python 3删除一些输出内容

标签 python python-3.x

我得到的输出如下:

Review: This hotel has been here awhile. However, they’ve kept it up nicely. The staff is very professional and friendly. The rooms have everything you need. Only con, the ice machines are on the 2nd and 8th floors only. Nice place, great location.
      0
0  None
Review: Thank you for taking a moment to share your experience. I am pleased to hear you found the hotel and staff to your liking. We look forward to welcoming you back to our hotel in the not too distant future.Sincerely,Andrea McLeodAssistant Front Office ManagerHilton New York Grand CentralAndrea.McLeod@Hilton.com 212-986-8800
      0
0  None
Review: I have wasted lot of time looking for a laundry service as there is none in the hotel, the ones they have leaves at saturdays 9:30 am, and then you don't have any other option (neither assistance from the desk). The shower doors open alone, so everything gets wet and there is no place to put your soap or shampoo... This is like a 2 stars hotel :S I hope my company can book me another one for my following stays.
      0
0  None
Review: Dear Manuel AThank you for having chosen our hotel for your trip to New York. We apologize that the door did not close properly. If you do return to the hotel please let us know if any issues that you may have and we will be more than happy to fix them. Thank you again for choosing the Hilton Grand Central. 
      0

我想删除从开始的第二段和第四段评论:感谢您花点时间分享您的经验。我很高兴......评论:亲爱的曼努埃尔A感谢您选择我们的酒店作为您的纽约之旅。 ..

如何使用 python3 从输出中删除这两段?

这是代码的更新版本,工作正常。但是如何使用 CSV 格式的 panadas 将输出保存为数据帧格式?

for dtags in html.find_all('div', attrs={'class':'wrap'}):
        for index, ptags in enumerate(dtags.find_all('p', attrs={'class':'partial_entry'})):
            if index == 0: #match the first element
                x = ptags.text
                z = print('Review:', x)

最佳答案

看来您正在从 tripadvisor 解析此页面.

与其解析输出,不如更准确地选择 <p>页面中的元素。这些评论中的每一条(以及所有者的回复)都属于一个名为 wrap 的类。 ,所以我们可以找到所有这些 div,然后找到 partial_entry 的第一个匹配项类,而不是在选择所有内容后试图弄清楚我们是在查看回复还是评论。

for dtags in html.find_all('div', attrs={'class':'wrap'}):
    for index, ptags in enumerate(dtags.find_all('p', attrs={'class':'partial_entry'})):
        if index == 0: #match the first element
            x = ptags.text
            z = print('Review:', x)

关于python - 使用Python 3删除一些输出内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48742452/

相关文章:

python - Django CMS错误: modules are not found despite being installed

python - 如何将字符串变量从一个函数传递到另一个函数?

python - 使用 python : KeyError: 'O' 解析文件时出现逻辑错误

python - del self vs self.__del__() - 在 python 中清理的正确方法是什么?

python - 在python中使用多个进程记录到一个文件

java - 将 String 转换为 readUTF() 期望的格式

Python 重新连接到 MySQLdb

python - 如何使 random.choices 真正随机(我在 secrets 模块中找不到它)

python-3.x - Gunicorn禁用超时

python - 在 Cython 代码中使用 float 文字而不是 double?