【问题标题】:How can I scrape a WhatsApp emoji using BeautifulSoup?如何使用 BeautifulSoup 抓取 WhatsApp 表情符号?
【发布时间】:2021-10-24 00:54:14
【问题描述】:

我知道如何从 WhatsApp 抓取表情符号,但前提是:

  1. 只有一个表情符号没有任何文字或
  1. 有带有表情符号的文字。

但是当消息中有两个没有任何文字的表情符号时,我无法进行网络抓取。这是消息“??????”的html

<div class="JwMbj i0jNr selectable-text copyable-text">
    <span class="_3R6rC">
        <img crossorigin="anonymous"
            src="/img/d07f9aca6938f691b840f97dd1cd67dd_w_638-64.png" alt="????" draggable="false"
            class="_2UdhN _1xeoG i0jNr selectable-text copyable-text" data-plain-text="????"
            style="visibility: visible;">
    </span>
</div>

我尝试了这段代码来获取表情符号:

m = s.find_all('div', attrs={'class':'i0jNr'})
v = m.find('span', attrs={'class':'_3R6rC'})                         
for i in v.children:
    if isinstance(i, NavigableString):
        print(i)
    elif isinstance(i, Tag):
        print(i.attrs['alt'])

但是通过这段代码,这仅在有单个表情符号时才有效,但是当消息中有两个表情符号时,它只打印一个,例如消息是“????????”它的输出为“??????” (它只打印第一个表情符号)。这是该消息的 html

<div class="JwMbj i0jNr selectable-text copyable-text">
    <span class="_3R6rC">
        <img crossorigin="anonymous"
            src="/img/d07f9aca6938f691b840f97dd1cd67dd_w_1749-40.png" alt="????" draggable="false"
            class="_2UdhN _3zyju i0jNr selectable-text copyable-text" data-plain-text="????"
            style="visibility: visible;">
    </span>
    <span class="_3R6rC">
        <img crossorigin="anonymous"
            src="/img/d07f9aca6938f691b840f97dd1cd67dd_w_1845-40.png" alt="????" draggable="false"
            class="_2UdhN _3zyju i0jNr selectable-text copyable-text" data-plain-text="????"
            style="visibility: visible;">
    </span>
</div>

我尝试使用此代码打印两个表情符号,但它不起作用:

msglist = []
m = s.find_all('div', attrs={'class':'i0jNr'}) 
for b in m:
    v = b.find_all('div', attrs={'class':'JwMbj'})   
    for x in v:      
        z = x.find_all('span', attrs={'class':'_3R6rC'})                
        for i in z.children:
            if isinstance(i, NavigableString):
                print(i)
            elif isinstance(i, Tag):
                print(i.attrs['alt'])

但它没有给出任何输出。

【问题讨论】:

    标签: python python-3.x image web-scraping beautifulsoup


    【解决方案1】:

    您可以将&lt;img&gt; 标签转换为纯文本,然后使用.get_text 正常获取文本。例如:

    from bs4 import BeautifulSoup
    
    html_doc = """
    <div class="JwMbj i0jNr selectable-text copyable-text">
        <span class="_3R6rC">
            <img crossorigin="anonymous"
                src="/img/d07f9aca6938f691b840f97dd1cd67dd_w_1749-40.png" alt="?" draggable="false"
                class="_2UdhN _3zyju i0jNr selectable-text copyable-text" data-plain-text="?"
                style="visibility: visible;">
        </span>
        <span class="_3R6rC">
            <img crossorigin="anonymous"
                src="/img/d07f9aca6938f691b840f97dd1cd67dd_w_1845-40.png" alt="?" draggable="false"
                class="_2UdhN _3zyju i0jNr selectable-text copyable-text" data-plain-text="?"
                style="visibility: visible;">
        </span>
    </div>
    """
    
    soup = BeautifulSoup(html_doc, "html.parser")
    
    # select the main text div
    text_div = soup.select_one(".copyable-text")
    
    # convert all <img> to plain-text:
    for img in text_div.select("img[data-plain-text]"):
        img.replace_with(img["data-plain-text"])
    
    # get text normally:
    
    print(text_div.get_text(strip=True))
    

    打印:

    ??
    

    【讨论】:

    • 兄弟如果有很多消息如何获得这些输出?意味着只有在我们给出html时才有效,那么当有很多msg时如何获得输出?表示如果发件人的聊天中有四个消息 1)"??" 2)"??" 3)"??" 4)"??" 然后我尝试了这段代码@AndrejKesely
    • from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from bs4 import BeautifulSoup@Andrej Kesely
    • driver = webdriver.Chrome(executable_path=r'C:\Users\PRANAV PATIL\Downloads\chromedriver.exe') driver.get(r'https://web.whatsapp.com/') input("enter any key :") searchbox = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[@id='side']//div//div//label//div//div[@contenteditable='true']"))) searchbox.send_keys('Diksha') #enter your sender's name searchbox.send_keys(Keys.RETURN) input("enter any key :") html_doc = driver.page_source soup = BeautifulSoup(html_doc, "html.parser") ss = soup.find_all('div', attrs={'class':'message-out'})@Andrej Kesely
    • if len(ss) &gt; 4: ss = ss[-4:] for s in ss: n = s.find('span', attrs={'class':'i0jNr'}) text_div = n.select_one(".copyable-text") for img in text_div.select("img[data-plain-text]"): img.replace_with(img["data-plain-text"]) print(text_div.get_text(strip=True))@Andrej Kesely
    • 这给出了错误AttributeError: 'NoneType' object has no attribute 'select_one'@Andrej Kesely
    猜你喜欢
    • 2020-03-01
    • 2019-09-17
    • 1970-01-01
    • 2018-03-08
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多