抓取没有Chrome GUI的JS呈现的网站?

时间:2019-05-14 07:35:46

标签: python selenium web-scraping beautifulsoup phantomjs

我正在尝试使用selenium和BeautifulSoup抓取js呈现的网站。该代码工作正常,但我需要在没有任何镶边的服务器上运行它。在没有GUI的情况下,我应该更改哪些代码?

下面是当前代码:

from bs4 import BeautifulSoup
import selenium.webdriver as webdriver
import json
from selenium.webdriver.common.keys import Keys

url = 'https://www.bigbasket.com/pc/fruits-vegetables/fresh-vegetables/?nc=nb'

chromepath = "/Users/Nitin/Desktop/Milkbasket/Scraping/chromedriver"
driver = driver = webdriver.Chrome(chromepath)
driver.get(url)

#rest of code for fetching prices

1 个答案:

答案 0 :(得分:3)

我建议您放弃Selenium方法,并使用内置的urllib库,或者如果可能的话,使用requests库来获取所需的信息。可以从返回的JSON数据中获取所有产品的信息。例如:

import requests
import re

params = {
    "type" : "pc",
    "slug" : "fresh-vegetables",
    "tab_type" : '["all"]',
    "sorted_on" : "popularity",
    "listtype" : "pc",
}

session = requests.Session()

for page in range(1, 10):
    params['page'] = page
    req_vegetables = session.get("https://www.bigbasket.com/product/get-products", params=params)
    json_vegetables = req_vegetables.json()

    print(f'Page {page}')

    for product in json_vegetables['tab_info']['product_map']['all']['prods']:
        print(f"  {product['p_desc']} - {product['sp']} - {product['mrp']}")

这将为您提供以下输出:

Page 1
  Onion - 21.00 - 26.25
  Potato - 27.00 - 33.75
  Tomato - Hybrid - 40.00 - 50.00
  Ladies Finger - 10.00 - 12.50
  Cauliflower - 35.00 - 43.75
  Palak - 30.00 - 37.50
  Potato Onion Tomato 1 kg Each - 88.00 - 110.00
  Carrot - Local - 59.00 - 73.75
  Capsicum - Green - 89.00 - 111.25
  Tomato - Local - 47.00 - 58.75
  Mushrooms - Button - 49.00 - 61.25
  Cucumber - 25.00 - 31.25
  Broccoli - 18.40 - 23.00
  Bottle Gourd - 17.00 - 21.25
  Cabbage - 32.00 - 40.00
  Cucumber - English - 23.00 - 28.75
  Tomato - Local, Organically Grown - 29.00 - 36.25
  Brinjal - Bottle Shape - 72.00 - 90.00
  Onion - Organically Grown - 23.00 - 28.75
  Methi - 19.00 - 23.75
Page 2
  Bitter Gourd / Karela - 59.20 - 74.00
  Beetroot - 40.00 - 50.00
  Fresho Palak - Without Root 250 Gm + Amul Malai Paneer 200 Gm - 94.20 - 102.00
  Capsicum - Red - 299.00 - 373.75 
  ... etc