数据分析与数据挖掘 笔记1 在学chromdriver谷歌驱动时,执行以下代码
from selenium import webdriver
import time
import requests
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as bs
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get("https://www.baidu.com")
driver.find_element_by_id('kw').send_keys('美团外卖')
driver.find_element_by_id('kw').send_keys(Keys.ENTER)
time.sleep(3)
textHtml = (driver.page_source)
print(driver.page_source)
soup = bs(textHtml,'html.parser')
all_links = soup.find_all('h3',class_='t')
for i in all_links:
url=(i.a.attrs['href'])
new_url=requests.get(url=url).url
time.sleep(1.5)
print("*"*100)
print(new_url)
遇到如下错误 首先确保自己的驱动下载是符合谷歌版本的,参考文章 也可以参考这篇文章关闭谷歌的自动更新 那么接下来的解决方案–使用绝对路径-调取谷歌驱动 r’xx\chromedriver.exe’
driver = webdriver.Chrome(executable_path=r'D:\lession\demo\demo\chromedriver.exe')
运行成功
如何获取chromdrive的绝对路径 点进去第一个r’绝对路径’
|