Web scrape get drop-down menu data python

2024/11/15 8:53:52

I am trying to get a list of all countries in the webpage https://www.nexmo.com/products/sms. I see the list is displayed in the drop-down. After inspecting the page, I tried the following code but I must be doing something wrong. I would appreciate some help here.

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)
Answer

This webpage uses JavaScript to render the HTML. You can render it with Selenium. First install Selenium.

sudo pip3 install selenium

Then get a driver https://sites.google.com/a/chromium.org/chromedriver/downloads (Depending upon your OS you may need to specify the location of your driver)

from selenium import webdriver
from bs4 import BeautifulSoupbrowser = webdriver.Chrome()
url = ('https://www.nexmo.com/products/sms')
browser.get(url)
html_source = browser.page_source
browser.quit()
soup = BeautifulSoup(html_source, 'html.parser')
for name_list in soup.find_all(class_ ='dropdown-row'):print(name_list.text)

Outputs:

Afghanistan
Albania
...
Zambia
Zimbabwe

UPDATED

Alternatively use PyQt5:

On Ubuntu

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

Other OS:

pip3 install PyQt5

Then run:

from bs4 import BeautifulSoup
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineViewclass Render(QWebEngineView):def __init__(self, url):self.html = Noneself.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 = dataself.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)
https://en.xdnf.cn/q/120463.html

Related Q&A

TypeError(unsupported operand type(s) for ** or pow(): str and int,)

import mathA = input("Enter Wright in KG PLease :") B = input("Enter Height in Meters Please :")while (any(x.isalpha() for x in A)):print("No Letters Please")A = input(&qu…

How can I do assignment in a List Comprehension? [duplicate]

This question already has answers here:How can I do assignments in a list comprehension?(8 answers)Closed 1 year ago.Generally, whenever I do a for loop in python, I try to convert it into a list comp…

KeyError: column_name

I am writing a python code, it should read the values of columns but I am getting the KeyError: column_name error. Can anyone please tell me how to fix this issue. import numpy as np from sklearn.clust…

Find starting and ending indices of list chunks satisfying given condition

I am trying to find the start and stop indices of chunks of positive numbers in a list.cross = [7,5,8,0,0,0,0,2,5,8,0,0,0,0,8,7,9,3,0,0,0,3,2,1,4,5,0,0,0,7,5] For the given example input, the desired o…

How can I Scrape Business Email Contact with python?

this morning I wanted to create a little Software/Script in Python, it was 6am when I started and now Im about to become crazy because its 22pm and I have nothing that works.So basically, I want to do …

Python class that works as list of lists

Im trying to create a python class that can work as a list of lists. However, all Ive managed to develop so far is,class MyNestedList(list): ...Im aware that the above code will work as,my = MyNestedLi…

GPA Python Assignment [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Tuple Errors Python

I opened Python and attempted to run the following script (btw, this script is what was directly given to me, I havent edited it in any way as its part of an assignment aside from entering the username…

Flatten list of lists within dictionary values before processing in Pandas

Issue:If I need to flatten a list of lists I use something like this list comprehension to flatten into a single list:[item for sublist in l for item in sublist]I have a dictionary where some of the va…

how to analyse and predict(machine learning) a time series data set using scikit-learn for python

i got data-set like this i need to analyse and predict the status column. This is just 2 entrees from the training data set. In this data set there is heart rate pattern(which is collected in 1 second …