[英]Beautiful Soup find is returning none
發現返回“無”
這是我嘗試過的所有代碼以及我正在使用的 html:
url = "https://www.instagram.com/p/BszEBehhwet/"
a = urlopen(url)
html = a.read()
a.close()
page_soup = soup(html, "html.parser")
found = page_soup.find("div", {"class":"P9YgZ"})
<div class="KlCQn G14m- EtaWk">
<ul class="k59kT">
<li class="gElp9 " role="menuitem">
<div class="P9YgZ">
<div class="C7I1f X7jCj">
<div class="C4VMK">
<h2 class="_6lAjh">
<a class="FPmhX notranslate TlrDj"
title="ray.walker00"
href="/ray.walker00/">ray.walker00
</a>
</h2>
<span>Jan. 18, 2019 // Awesome
</span>
</div>
</div>
</div>
</li>
</ul>
</div>
我想返回 div 類 P9YgZ
正如我在評論中所述,您正在使用的頁面非常依賴於 javascript,因此urllib
本身不會削減它。 這是一個使用 Selenium WebDriver 的示例,該示例獲取具有該類的元素。 您需要下載ChromeDriver並修改代碼以將其指向系統上的位置:
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def main():
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(
options=options, executable_path="C:\chromedriver\chromedriver.exe"
)
try:
driver.get("https://www.instagram.com/p/BszEBehhwet/")
soup = BeautifulSoup(driver.page_source, "html.parser")
print(soup.find("div", {"class": "P9YgZ"}))
finally:
driver.quit()
if __name__ == "__main__":
main()
結果:
<div class="P9YgZ"><div class="C7I1f X7jCj"><div class="C4VMK"><h2 class="_6lAjh"><a class="FPmhX notranslate TlrDj" href="/thetremason/" title="thetremason">thetremason</a></h2><span>How I’m finna pull up to ya function.</span></div></div></div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.