我是Python的初学者。我试图做一个解析器。但是我认为chromdriver有问题。我需要一些帮助。运行代码:
from bs4 import BeautifulSoup
import csv, sys, os, urllib.request, requests, datetime, time
##import pandas as pd
##from pandas import read_csv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.options import Options
pang_list = [ 'http://www.amcal.com.au/brands/our-john-frieda-range',
'http://www.amcal.com.au/brands/our-oral-b-range',
'http://www.amcal.com.au/brands/our-tommee-tippee-range',]
def driv():
chromedriver = "chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chrome_options)
return driver
def get_links(soup):
box = []
block = soup.find_all('div',{'class':'product_info'})
for aa in block:
obj = aa.find('a').get('href')
box.append(obj)
df = pd.DataFrame(box, columns=['Link'])
if os.path.exists('links.csv'): head = False
else: head = True
df.to_csv('links.csv',sep=';', index=False, encoding='utf-8-sig', mode='a', header=head)
def max_rows(driver):
driver.find_element_by_xpath('//*[@id="pageSizeTop"]/tbody/tr/td[2]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="dijit_MenuItem_2_text"]').click()
time.sleep(5)
soup = BeautifulSoup(driver.page_source)
return soup
driver = driv()
for ii in pang_list[:]:
print(ii)
driver.get(ii)
soup = max_rows(driver)
get_links(soup)
nn = 1
while True:
try:
driver.find_element_by_xpath('//*[@id="WC_SearchBasedNavigationResults_pagination_link_right_categoryResults_Top"]').click()
time.sleep(5)
soup_ = BeautifulSoup(driver.page_source)
get_links(soup_)
print(nn)
nn += 1
except: break
终端发出错误:
selenium.common.exceptions.SessionNotCreatedException:消息:会话未从标签页创建异常
我的系统:
Chrome版本。 67 chromedriver 2.39版 硒ver.3 python 3.7 赢32
我试图以管理员身份运行。但这没有用。 我很乐意为您提供帮助。