[英]beautiful soup .find can't find anything
I am trying to scrape posts in a Facebook group:我正在尝试抓取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()
The div
with class="text_exposed_root"
is clearly there because I can find it with CTRL f when I search in print(soup.prettify())
, but when I do soup.find_all("div", {"class_": "text_exposed_root"})
it is returning an empty list, so are many other class names that are clearly there.带有
class="text_exposed_root"
的div
显然在那里,因为当我在print(soup.prettify())
搜索时,我可以用CTRL f找到它,但是当我做soup.find_all("div", {"class_": "text_exposed_root"})
它返回一个空列表,因此许多其他类名显然存在。
Please help.请帮忙。
The problem is that all those <div>
are inside a commented out HTML block.问题是所有那些
<div>
都在一个注释掉的 HTML 块中。
Something like this can workaround the issue:这样的事情可以解决这个问题:
soup = BeautifulSoup(page.text.replace('<!--', '').replace('-->', ''), 'html.parser')
After that you can simply do:之后,您可以简单地执行以下操作:
posts = soup.find_all('div', 'text_exposed_root')
I hope it helps.我希望它有帮助。
You only need to use class_
when you're checking the class using a keyword argument, because class
is a Python reserved word and can't be used as a variable.只需要在使用关键字参数检查类时使用
class_
,因为class
是 Python 保留字,不能用作变量。 If you pass the attributes as a dictionary, you just use class
.如果将属性作为字典传递,则只需使用
class
。
So it should be所以应该是
posts = soup.find_all("div", {"class": "text_exposed_root"})
or或者
posts = soup.find_all("div", class_ = "text_exposed_root")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.