Python beautifulsoup没有在网站上工作

Python beautifulsoup没有在网站上工作,python,python-3.x,web-scraping,beautifulsoup,web-crawler,Python,Python 3.x,Web Scraping,Beautifulsoup,Web Crawler,这可能是因为网站是通过javascript加载的。尝试使用硒而不是美素 from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen("https://homeshopping.pk/categories/Mobile-Phones-Price-Pakistan") soup = BeautifulSoup(html,features="lxml") x = soup.find("div",{"cl

这可能是因为网站是通过javascript加载的。尝试使用硒而不是美素

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("https://homeshopping.pk/categories/Mobile-Phones-Price-Pakistan")
soup = BeautifulSoup(html,features="lxml")
x = soup.find("div",{"class":"innerp product-box  Even  product_300564"})
print(x)

您做得对,但只需在类定义之前和之后删除额外的空格即可。 像这样:

from selenium import webdriver

chrome_driver_path = 'chromedriver.exe'
driver = webdriver.Chrome(chrome_driver_path)
driver.get("https://homeshopping.pk/categories/Mobile-Phones-Price-Pakistan")

element = driver.find_element_by_css_selector('div.innerp.product-box.Even.product_300564')
print(element.text)


>>> element.text
'Samsung Galaxy A51 (4G, 6GB, 128GB, Prism Black) With Official Warranty\nRs 53,999'

您可能已经从html中复制了类定义,但它似乎美化了它,使其不包含空格。

但是为什么beautifull soup不这样做?beautifull soup正在这样做。请看我的答案-我测试过了。
x = soup.find("div",{"class":"innerp product-box Even product_300564"})