很好的汤。什么都找不到

2024-04-20 13:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Facebook群中删除帖子:

URL = 'https://www.facebook.com/groups/110354088989367/'

headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'
}


def checkSubletGroup():
    page = requests.get(URL, headers=headers)
    soup = BeautifulSoup(page.content, 'html.parser')
    posts = soup.find_all("div", {"class_": "text_exposed_root"})
    print(soup.prettify())
    for post in posts:
        print(post)


checkSubletGroup()

带有class="text_exposed_root"div明显存在,因为当我在print(soup.prettify())中搜索时,我可以使用CTRLf找到它,但是当我这样做soup.find_all("div", {"class_": "text_exposed_root"})时,它会返回一个空列表,其他许多类名也明显存在

请帮忙


Tags: textdivurlpagerootallfindprettify
2条回答

问题是所有这些<div>都在注释掉的HTML块中

这样的方法可以解决这个问题:

soup = BeautifulSoup(page.text.replace('<! ', '').replace(' >', ''), 'html.parser')

之后,您可以简单地执行以下操作:

posts = soup.find_all('div', 'text_exposed_root')

希望对你有帮助

在使用关键字参数检查类时,只需要使用class_,因为class是Python保留字,不能用作变量。如果将属性作为字典传递,只需使用class

所以应该是这样

posts = soup.find_all("div", {"class": "text_exposed_root"})

或者

posts = soup.find_all("div", class_ = "text_exposed_root")

相关问题 更多 >