詳解用python實(shí)現(xiàn)爬取CSDN熱門評(píng)論URL并存入redis
下載谷歌瀏覽器驅(qū)動(dòng),并配置好
import timeimport randomfrom PIL import Imagefrom selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECif __name__ == ’__main__’: options = webdriver.ChromeOptions() options.binary_location = r’C:UsershhhAppDataLocalGoogleChromeApplication谷歌瀏覽器.exe’ # driver=webdriver.Chrome(executable_path=r’D:360Chromechromedriverchromedriver.exe’) driver = webdriver.Chrome(options=options) #以java模塊為例 driver.get(’https://www.csdn.net/nav/java’) for i in range(1,20): driver.execute_script('window.scrollTo(0, document.body.scrollHeight)') time.sleep(2)二、獲取URL
from bs4 import BeautifulSoupfrom lxml import etree html = etree.HTML(driver.page_source)# soup = BeautifulSoup(html, ’lxml’)# soup_herf=soup.find_all('#feedlist_id > li:nth-child(1) > div > div > h2 > a')# soup_herftitle = html.xpath(’//*[@id='feedlist_id']/li/div/div/h2/a/@href’)
可以看到,一下爬取了很多,速度非常快
導(dǎo)入redis包后,配置redis端口和redis數(shù)據(jù)庫(kù),用rpush函數(shù)寫入打開redis
import redisr_link = redis.Redis(port=’6379’, host=’localhost’, decode_responses=True, db=1)for u in title: print('準(zhǔn)備寫入{}'.format(u)) r_link.rpush('csdn_url', u) print('{}寫入成功!'.format(u))print(’=’ * 30, ’n’, '共計(jì)寫入url:{}個(gè)'.format(len(title)), ’n’, ’=’ * 30)
大功告成!
在Redis Desktop Manager中可以看到,爬取和寫入都是非常的快。
要使用只需用rpop出棧就OK
one_url = r_link.rpop('csdn_url)')while one_url: print('{}被彈出!'.format(one_url))
到此這篇關(guān)于詳解用python實(shí)現(xiàn)爬取CSDN熱門評(píng)論URL并存入redis的文章就介紹到這了,更多相關(guān)python爬取URL內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. vue實(shí)現(xiàn)web在線聊天功能2. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法3. Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法4. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)5. Springboot 全局日期格式化處理的實(shí)現(xiàn)6. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. Django使用HTTP協(xié)議向服務(wù)器傳參方式小結(jié)9. Java使用Tesseract-Ocr識(shí)別數(shù)字10. JAMon(Java Application Monitor)備忘記
