第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

捕獲列表標(biāo)簽之間的文本并從 BeautifulSoup scrape 打印

捕獲列表標(biāo)簽之間的文本并從 BeautifulSoup scrape 打印

滄海一幻覺 2022-06-02 14:38:53
剛剛開始使用 BeautifulSoup 和 Requests 進(jìn)行 Web Scraping。我正在嘗試創(chuàng)建一個(gè)腳本,可以在此處的有序列表中抓取消息我被困在如何打印那里列出的消息的第 2 行這是我到目前為止的腳本。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())該腳本僅打印出整個(gè)文本。我該如何打印文本 2 或文本 3 等等...提前致謝。編輯:這是我運(yùn)行腳本時(shí)得到的結(jié)果。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> 我實(shí)際上是想打印出<li>標(biāo)簽上的下一個(gè)內(nèi)容。
查看完整描述

3 回答

?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

嘗試使用 find_all 來獲取結(jié)果列表。find 只返回它找到的第一件事。嘗試在 li 上查找所有內(nèi)容



查看完整回答
反對(duì) 回復(fù) 2022-06-02
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以使用更快的類選擇器來獲取父級(jí) 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)

如果在某個(gè)時(shí)候想要所有行但值得了解nth-of-type和相關(guān)技巧,則索引整個(gè)列表會(huì)更快。


查看完整回答
反對(duì) 回復(fù) 2022-06-02
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊

要獲取每個(gè)引用中的文本列表,請(qǐng)使用您已經(jīng)隔離findAll()的塊上的方法。ol


ol = soup.find('ol') 

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

現(xiàn)在您可以通過它們的索引訪問消息。請(qǐng)記住,列表的索引為 0,這意味著項(xiàng)目 1 實(shí)際上 = 0。


print(messages[0]) # Actually the first message

# output: Meeting you was the best day of my life.


查看完整回答
反對(duì) 回復(fù) 2022-06-02
  • 3 回答
  • 0 關(guān)注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)