如何使用findAll

时间:2018-02-27 14:17:00

标签: python web-scraping beautifulsoup subprocess python-requests

FindAll找不到我需要的课程。但是我能够找到那个以上的类,但数据结构组织得不好。

  • 您知道我们可以做些什么来获取数据或组织上述类中包含所有数据的输出吗?

请参阅下面的HTML和图片。

import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.vivino.com/explore?e=eJzLLbI11jNVy83MszU0UMtNrLA1MVBLrrQtLVYrsDVUK7ZNTlQrS7YtKSpNVSsviY4FioEpIwhlDKFMIJQ5VM4EAJCfGxQ='

#Opening a connection
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

#html parse
page_soup = soup(page_html, "html.parser")
container = page_soup.findAll("div", {"class":"wine-explorer__results__item"})
len(container)

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以将Dryscrape模块与bs4一起使用,因为wine-explorer选择器是由javascript创建的。 Dryscrape模块可以帮助您获得javascript支持。

答案 1 :(得分:0)

请尝试使用以下内容:

container = page_soup.findAll("div", {"class": "wine-explorer__results"})

答案 2 :(得分:0)

谢谢大家,因为你们都建议用一个模块来阅读Javascript是需要选择那个类的。我在这种情况下使用过selenium,但是PyQt5可能是更好的选择。

import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
from selenium import webdriver

my_url = 'https://www.vivino.com/explore?e=eJzLLbI11jNVy83MszU0UMtNrLA1MVBLrrQtLVYrsDVUK7ZNTlQrS7YtKSpNVSsviY4FioEpIwhlDKFMIJQ5VM4EAJCfGxQ='

#Opening a connection

#html parse

web_r = uReq(my_url)

driver=webdriver.Firefox()
driver.get(my_url)
page_soup = soup(web_r, "html.parser")
html = driver.execute_script("return document.documentElement.outerHTML")

#print(html)
html_page_soup = soup(html, "html.parser")
container = html_page_soup.findAll("div", {"class": "wine-explorer__results__item"})
len(container)