我正在構建一個 Facebook Group Scraper,我已經設法撰寫代碼來登錄 刮取名稱,但由于某種原因,我的代碼只回傳一個結果而不是所有帖子我想要的頁面。
這是我的代碼:
for result in driver.find_elements_by_xpath('//div[@]'):
poster = result.find_element_by_xpath('//a[@]/strong/span').text
description = result.find_element_by_xpath('//div[@]').text
groupcomments.append({
'poster' : poster,
'description' : description,
})
print(groupcomments)
這是 Facebook 源代碼的片段(你可以在這里找到它:https : //www.facebook.com/groups/286175922122417)
<div data-pagelet="GroupFeed"><div class="j83agx80 l9j0dhe7 k4urcfbm"><div class="rq0escxv l9j0dhe7 du4w35lb hybvsw6c io0zqebd m5lcvass fbipl8qg nwvqtn77 k4urcfbm ni8dbmo4 stjgntxs sbcfpzgs" style="border-radius: max(0px, min(8px, ((100vw - 4px) - 100%) * 9999)) / 8px;"><div class="ihqw7lf3"><div class="rq0escxv l9j0dhe7 du4w35lb j83agx80 cbu4d94t pfnyh3mw d2edcug0 e5nlhep0 aodizinl"><div class="rq0escxv l9j0dhe7 du4w35lb j83agx80 cbu4d94t buofh1pr tgvbjcpo"><div class="rq0escxv l9j0dhe7 du4w35lb j83agx80 cbu4d94t pfnyh3mw d2edcug0 hv4rvrfc dati1w0a"><div class="j83agx80 cbu4d94t ew0dbk1b irj2b8pg"><div class="qzhwtbm6 knvmm38d"><span class="d2edcug0 hpfvmrgz qv66sw1b c1et5uql oi732d6d ik7dh3pa ht8s03o8 a8c37x1j keod5gw0 nxhoafnm aigsh9s9 d9wwppkn fe6kdd0r mau55g9w c8b282yb iv3no6db a5q79mjw g1cxx5fr lrazzd5p oo9gr5id" dir="auto"><div class="rq0escxv l9j0dhe7 du4w35lb j83agx80 pfnyh3mw i1fnvgqd bp9cbjyn owycx6da btwxx1t3 jeutjz8y"><div class="rq0escxv l9j0dhe7 du4w35lb j83agx80 cbu4d94t g5gj957u d2edcug0 hpfvmrgz rj1gh0hx buofh1pr"
有什么想法可以獲取我正在尋找的所有資訊嗎?提前致謝 :)
uj5u.com熱心網友回復:
我設法使用 BeautifulSoup HTML 抓取器抓取了我想要的內容,以使用我正在尋找的資訊的 xpath 簡單地抓取資訊(這不是 100% 萬無一失的解決方案,因為這些解決方案可以更改,但可以輕松地在代碼中替換,所以我想這總比沒有好......)
while True:
soup=BeautifulSoup(driver.page_source,"html.parser")
all_posts=soup.find_all("div",{"class":"du4w35lb k4urcfbm l9j0dhe7 sjgh65i0"})
for post in all_posts:
try:
name=post.find("a",{"class":"oajrlxb2 g5ia77u1 qu0x051f esr5mh6w e9989ue4 r7d6kgcz rq0escxv nhd2j8a9 nc684nl6 p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x jb3vyjys rz4wbd8a qt6c0cv9 a8nywdso i1ao9s8h esuyzwwr f1sip0of lzcic4wl oo9gr5id gpro0wi8 lrazzd5p"}).get_text()
except:
name="not found"
print(name)
如果你想要更深入的教程,我還制作了一個視頻來展示我是如何撰寫它的,你可以在這里觀看(你也可以在視頻描述中找到完整的代碼)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355701.html
