【问题标题】:Capture text between list tags and print from BeautifulSoup scrape捕获列表标签之间的文本并从 BeautifulSoup scrape 打印
【发布时间】:2020-01-23 02:04:57
【问题描述】:

刚刚开始使用 BeautifulSoup 和 Requests 进行网页抓取。 我正在尝试创建一个脚本,可以在有序列表here上抓取消息

我不知道如何打印列出的消息的第 2 行 there

这是我到目前为止的脚本。

from bs4 import BeautifulSoup 
import requests 

res = requests.get("https://www.serenataflowers.com/pollennation/love-text-messages/")

soup = BeautifulSoup(res.text, 'html.parser')  

ol = soup.find('ol') 
print(ol.prettify())

脚本只打印出整个文本。我如何去打印文本 2 或文本 3 等等......

提前致谢。

编辑:这是我运行脚本时得到的结果。

C:\Users\XXXX\MyPythonScripts>scrape.py

<ol class="simple-list">
 <li>
  Meeting you was the best day of my life.
 </li>
 <li>
  When you are next to me, or when we are apart, You are always the first in my thoughts and in my heart.
 </li>
 <li>
  I never ever thought I’d like you this much and I never planned to have you on my mind this often.
 </li>
 <li>
  I love the way you love me.
 </li>
 <li>
  Spring drops and the sun outside the window tell me that this spring will be the flowering of our love.
 </li>
 <li>
  I can’t spend a day without you, can’t you see? I love you so much. You are a part of me and this is forever.
 </li>
 <li>
  You make me happy in a thousand ways. I love you to the moon and back, and I have no idea what I would do, if I lost you, because I feel like I will lose my entire world.
 </li>
 <li>
  Nothing is going change my love for you, you are the man, who helped me to find myself in this life.
 </li>
 <li>
  I can’t imagine living a life without you. You are my reason to be.
 </li>
 <li>
  The wind whispers your name, stars illuminate my way to you, we will meet soon, love you!
 </li> 

我实际上是想打印出&lt;li&gt; 标签上的下一个内容。

【问题讨论】:

  • 你能编辑并添加一个你现在得到的文本的例子,以及你想要得到的文本吗?
  • 已编辑。您现在可以查看。
  • 所以你想要的输出只是文本而不是列表中的html标签部分或其他东西?
  • 是的!而且我还想了解如何在没有
  • 标签的情况下打印下一行消息。
  • 试试看this answer
  • 标签: python beautifulsoup python-requests


    【解决方案1】:

    尝试使用 find_all 获取结果列表。 find 只返回它找到的第一件事。尝试在 li 上查找所有内容

    【讨论】:

    • 是的,即使我找到或 find_all,它也会给我相同的结果。我实际上正在寻找一种方法来打印
    • 标签上的下一项。我已经对问题进行了编辑,您可以检查我的结果。
    【解决方案2】:

    要获取每个引用中的文本列表,请在您已隔离的 ol 块上使用 findAll() 方法。

    ol = soup.find('ol') 
    messages = [msg.text for msg in ol.findAll('li')]  # this goes through and isolates the text of each message
    

    现在您可以通过索引访问消息。请记住,列表是 0 索引的,这意味着项目 1 实际上 = 0。

    print(messages[0]) # Actually the first message
    # output: Meeting you was the best day of my life.
    

    【讨论】:

    • 成功了!您能否解释一下您是如何找到该循环的答案的?
    • 你有没有特别困惑的部分?
    【解决方案3】:

    您可以使用更快的类选择器来获取父级 ol,然后使用 nth-of-type 来隔离特定行:

    import requests
    from bs4 import BeautifulSoup as bs
    
    r  = requests.get('https://www.serenataflowers.com/pollennation/love-text-messages/')
    soup = bs(r.content, 'lxml')
    line_number = 2
    print(soup.select_one(f'.simple-list li:nth-of-type({line_number})').text)
    

    如果在某个时候想要所有行但值得了解 nth-of-typerelated tricks,则从整个列表中索引会更快。

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签