日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫源代码_零基础自学爬虫(5)B站有哪些爬虫的视频学习资源-附Python源代码...

發布時間:2023/11/30 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫源代码_零基础自学爬虫(5)B站有哪些爬虫的视频学习资源-附Python源代码... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前幾天看到有人提問:。b站哪個python爬蟲視頻講的較好?謝謝各位能解答一下。?

于是順手寫了一個小爬蟲,把數據爬了下來。 今天有空放一下源代碼。

數據源,是在B站搜索框直接搜索“爬蟲”,并選擇按收藏排序。總結果大概50頁,然后爬取該數據。

第一步:啟動 chromedriver,無頭模式。

from time import sleep from datetime import datetime from selenium import webdriver from selenium.webdriver.chrome.options import Options import pandas as pd import re import json# 啟動 driver(無頭模式) def start_driver():chrome_options = Options()chrome_options.add_argument("--headless")chrome_options.add_argument("--disable-gpu")driver = webdriver.Chrome(options=chrome_options)return driver

第二步:爬取一頁搜索結果,封裝為函數。

# 獲取一頁搜索結果 def get_onepage(driver,url,n):driver.get(url)sleep(3)rlts = driver.find_elements_by_class_name("info")info = {}pttn = "(http.*?)?from"for rlt in rlts:t1 = rlt.find_element_by_xpath('./div[1]/a')v[n] = {"title":t1.text,"url":re.findall(pttn,t1.get_attribute("href"))[0]}t2 = rlt.find_element_by_xpath('./div[3]')x = t2.text.split('n')v[n]["watch_num"] = x[0]v[n]["update"] = x[1]v[n]["upper"] = x[2]n += 1return v,n

第三步:逐頁讀取所有數據。

driver = start_driver() n = 1 pages = 51 for i in range(1,pages):url = "https://search.bilibili.com/all?keyword=%E7%88%AC%E8%99%AB&from_source=nav_suggest_new&order=stow&duration=0&tids_"+str(i)try:v,n = get_onepage(driver,url,n)except:break

第四步:數據結果存儲。

# 數據保存為 json 文件 file = "bilibili_crawler.json" with open(file,"w",encoding="utf-8") as f:json.dump(v,f,indent=4,sort_keys=False, ensure_ascii=False)# 也可保存為表格文件 df = pd.DataFrame(v).T file2 = "bilibili_crawler.csv" df.to_csv(file2)

點贊,收藏,關注,分享四連。好人一生平安。

看不懂的,評論留言問我。

想要所有數據,站內私信我。

總結

以上是生活随笔為你收集整理的python爬虫源代码_零基础自学爬虫(5)B站有哪些爬虫的视频学习资源-附Python源代码...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。