参考文章:https://www.cnblogs.com/dcpeng/p/15335946.html
爬虫代码
#!python
# -*-coding:utf-8-*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
class Item:
page_num = ""
part = ""
duration = ""
def __init__(self, page_num, part, duration):
self.page_num = page_num
self.part = part
self.duration = duration
def get_second(self):
str_list = self.duration.split(":")
sum = 0
for i, item in enumerate(str_list):
sum += pow(60, len(str_list) - i - 1) * int(item)
return sum
def get_bilili_page_items(url):
options = webdriver.ChromeOptions()
options.add_argument('--headless') # 设置无界面
options.add_experimental_option('excludeSwitches', ['enable-automation'])
browser = webdriver.Chrome(chrome_options=options)
print("正在打开网页...")
browser.get(url)
print("等待网页响应...")
# 需要等一下,直到页面加载完成
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@class="list-box"]/li/a')))
print("正在获取网页数据...")
list = browser.find_elements_by_xpath('//*[@class="list-box"]/li')
# print(list)
itemList = []
second_sum = 0
# 2.循环遍历出每一条搜索结果的标题
for t in list:
# print("t text:",t.text)
element = t.find_element_by_tag_name('a')
# print("a text:",element.text)
arr = element.text.split('\n')
print(" ".join(arr))
item = Item(arr[0], arr[1], arr[2])
second_sum += item.get_second()
itemList.append(item)
print("总数量:", len(itemList))
print("总时长/分钟:", round(second_sum / 60, 2))
print("总时长/小时:", round(second_sum / 3600.0, 2))
browser.close()
return itemList
get_bilili_page_items("https://www.bilibili.com/video/视频号")
遇到的问题
“C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py”, line 73, in init
self.service.start()
File “C:\Python27\lib\site-packages\selenium\webdriver\common\service.py”, line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
分析:问题的原因是 webdriver版本不对,需要下载正确的 webdriver 版本,并且放置在正确的路径下。
解决
- 查看当前谷歌浏览器的版本,chrome://version/
- 到此页面下载正确的webdriver版本,http://chromedriver.storage.googleapis.com/index.html
- 把下载下来的插件放在 C:\Python27\Scripts目录下。(python 2.7)