当我说inspect pag时,我看不到类的实际名称

2024-04-24 06:14:20 发布

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

好吧,我只是在学习python,我想使用web抓取。我在看这个教程,那里的导师有一个完全不同的“检查”页(或无论它叫什么)比我的。所以他看到的是class=“ProfileHeaderCard”,我看到的是class=“css-1dbjc4n r-1iusvr4 r-16y2uox r-5f2r5o r-m611by”。重要的是,当我使用我的类名版本时,BeautifulSoup库不起作用,但当我使用他的版本时,它起作用。当我说print(soup.find('div', {"class":"css-1dbjc4n r-1iusvr4 r-16y2uox r-5f2r5o r-m611by"})) 它不返回任何值 怎么回事,哈哈,请帮帮我。你知道吗

from bs4 import BeautifulSoup
import urllib.request

theurl = 'https://twitter.com/1kasecorba'
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, 'html.parser')

print(soup.find('div', {"class":"css-1dbjc4n r-1iusvr4 r-16y2uox r-5f2r5o r-m611by"}))

Tags: import版本divwebrequestfindurllibcss
1条回答
网友
1楼 · 发布于 2024-04-24 06:14:20

它找不到它,因为它不在那里。请注意,当您对页面执行GET请求时,您通常无法获得与在浏览器中打开页面时看到的源代码相同的源代码(Control+U)。你知道吗

我编写了一个脚本,将urllib获取的源代码的内容写入一个文本文件,但没有您要查找的此类。这辆车没问题汤.找函数,您将在最后一行的示例中看到。你知道吗

from bs4 import BeautifulSoup
import urllib.request

theurl = 'https://twitter.com/1kasecorba'
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, 'html.parser')

file = open("page_source.txt", "w+", encoding="utf-8")
file.write(str(soup))
file.close()

# works as charm
print(soup.find('button', {"class":"modal-btn modal-close modal-close-fixed js-close"}))

如果您想看到真正的源代码,您将需要一个像Selenium这样的工具(可能有更好的选择,我不能在这个主题上给出太多建议)。你知道吗

相关问题 更多 >