Web scrape获取下拉菜单数据python

2024-05-21 08:40:26 发布

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

我正在尝试获取网页https://www.nexmo.com/products/sms中所有国家的列表。我看到列表显示在下拉列表中。在检查了页面之后,我尝试了下面的代码,但是我一定是做错了什么。我会很感激你的帮助。在

import requests
from bs4 import BeautifulSoup
# collect and parse page
page = requests.get('https://www.nexmo.com/products/sms')
soup = BeautifulSoup(page.text, 'html.parser')
# pull all text from the div
name_list = soup.find(class_ ='dropdown-content')
print(name_list)

Tags: textnamefromhttpsimportcom列表www
1条回答
网友
1楼 · 发布于 2024-05-21 08:40:26

此网页使用JavaScript呈现HTML。你可以用硒来渲染它。首先安装Selenium。在

sudo pip3 install selenium

然后获取一个驱动程序https://sites.google.com/a/chromium.org/chromedriver/downloads(根据您的操作系统,您可能需要指定驱动程序的位置)

^{pr2}$

输出:

Afghanistan
Albania
...
Zambia
Zimbabwe

更新

或者使用PyQt5:

在Ubuntu上

sudo apt-get install python3-pyqt5
sudo apt-get install python3-pyqt5.qtwebengine

其他操作系统:

pip3 install PyQt5

然后运行:

from bs4 import BeautifulSoup
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView


class Render(QWebEngineView):
    def __init__(self, url):
        self.html = None
        self.app = QApplication(sys.argv)
        QWebEngineView.__init__(self)
        self.loadFinished.connect(self._loadFinished)
        self.load(QUrl(url))
        self.app.exec_()

    def _loadFinished(self, result):
        self.page().toHtml(self.callable)

    def callable(self, data):
        self.html = data
        self.app.quit()

url = 'https://www.nexmo.com/products/sms'
html_source = Render(url).html
soup = BeautifulSoup(html_source, 'html.parser')
for name_list in soup.find_all(class_ ='dropdown-row'):
    print(name_list.text)

相关问题 更多 >