参考文章: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:Python27libsite-packagesseleniumwebdriverchromewebdriver.py”, line 73, in init self.service.start() File “C:Python27libsite-packagesseleniumwebdrivercommonservice.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 版本,并且放置在正确的路径下。
解决
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试