【问题标题】:How do I scrape food menu from zomato page?如何从 zomato 页面抓取食物菜单?
【发布时间】:2019-09-10 19:18:31
【问题描述】:

我正在尝试从 zomato 中抓取食物菜单数据。我在检查元素时使用 selenium 做同样的事情,我可以找到类“category_heading”,但在代码中使用相同的类没有结果并显示空列表。我附上了代码的sn-p。谢谢。

我尝试过使用 browser.find_element_by_xpath 以及 find_element_by_class_name 和 tag,但似乎没有任何效果。

order_now = browser.find_element_by_xpath("//*[@id='orig-search-list']/div[1]/div[2]/a").click()
browser.maximize_window() 
browser.implicitly_wait(20) 
food_item = browser.find_elements_by_class_name("category_heading")
print('food',food_item)

我需要食物菜单数据,以便将其存储在 csv 中。

【问题讨论】:

标签: python-3.x selenium selenium-webdriver beautifulsoup


【解决方案1】:

页面加载速度可能很慢。尝试使用等待条件

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as bs

d = webdriver.Chrome()
d.get('https://www.zomato.com/bangalore/burgers-kingdom-indiranagar-bangalore/order')
rows = WebDriverWait(d, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".row")))    
soup = bs(d.page_source, 'lxml')

for category in soup.select('.category-container'):
    title = category.select_one('h3').text
    print(title)
    items = [i.text for i in category.select('.category_heading')]
    if items:
        print(items)
    else:
        print('No sub-headers')

【讨论】:

  • 嘿...尝试使用等待条件,但仍然没有得到任何结果。这个脚本是为你运行的吗?
  • 是的,它对我来说运行完美。你有时间吗?
  • 它现在工作正常......这是正在发生的时间问题。谢谢
猜你喜欢
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多