分享VBA+Selenium网抓方法
vba吧
全部回复
仅看楼主
level 12
🌞Shawn 楼主
使用VBA+Selenium可以很便利地在浏览器上进行操作, 以及抓取一些需要的信息.
先分享一个例子, 抓取VBA贴吧的首页所有标题, 以及相应的每个链接, 效果图如下.
代码如下:
Option Explicit
'安装 Selenium 2.0.9
'配置当前版本的Chrome Driver
'参照Tools->Reference->Selenium Type Library
Public cd As Selenium.ChromeDriver
Sub TiebaList_Selenium()
Dim titles As Selenium.WebElements
Dim title As Selenium.WebElement
Dim url As String, row As Long
url = "https://tieba.baidu.com/f?kw=vba&ie=utf-8"
Set cd = New Selenium.ChromeDriver
With cd
.AddArgument "start-maximized" '窗口最大化
.Start
.Get url '访问网址
.FindElementById("head").WaitDisplayed '等待head显示
Set titles = .FindElementsByXPath("//*[@id='thread_list']//div/div[2]/div[1]/div[1]/a") '抓取首页
row = 1
For Each title In titles
''Debug.Print title.Text
Range("A" & row + 1) = title.Text
Range("B" & row + 1).Hyperlinks.Add Range("B" & row + 1), title.Attribute("href")
row = row + 1
Next title
.Quit '退出
End With
End Sub
2021年08月18日 09点08分 1
level 12
顶!
这个是网页浏览贴吧,没登陆状态下的?
2021年08月18日 12点08分 2
是的,使用谷歌浏览器打开VBA贴吧,不用登录也可抓取
2021年08月18日 14点08分
level 12
🌞Shawn 楼主
代码注释中的第一行应为:'安装SeleniumBasic 2.0.9
2021年08月18日 14点08分 3
level 12
🌞Shawn 楼主
今天再来分享一个使用VBA+Selenium在购物网站上面抓取VBA相关的图书价格.
主要功能: 输入关键字, 选择图书分类, 点击搜索按钮, 在表内写入第一页的60个搜索结果
代码分享:
'安装 SeleniumBasic 2.0.9
'配置当前版本的Chrome Driver
'参照Tools->Reference->Selenium Type Library
Public cd As Selenium.ChromeDriver
Sub Dangdang_Selenium()
Dim searchResults As Selenium.WebElements, rs As Selenium.WebElement
Dim prices As Selenium.WebElements, price As Selenium.WebElement
Dim url As String, row As Long
url = "http://www.dangdang.com/"
Set cd = New Selenium.ChromeDriver
With cd
.AddArgument "start-maximized" '窗口最大化
.Start
.Get url '访问网址
.FindElementById("key_S").SendKeys "VBA"
.FindElementByXPath("//*[@id='form_search_new']/span").Click '选择分类
.FindElementByXPath("//*[@id='search_all_category']/a[3]").Click
.FindElementByXPath("//*[@id='form_search_new']/input[10]").Click '点击搜索
Set searchResults = .FindElementsByName("itemlist-title")
Set prices = .FindElementsByXPath("//*/p[3]/span[1]")
'输出书名
row = 1
For Each rs In searchResults
Range("A" & row + 1) = rs.Text
row = row + 1
Next rs
'输出价格
row = 1
For Each price In prices
If Len(Range("A" & row + 1)) > 0 Then
Range("B" & row + 1) = price.Text
row = row + 1
Else
Exit For
End If
Next price
.Quit '退出
End With
End Sub
2021年08月23日 06点08分 4
level 12
🌞Shawn 楼主
欢迎吧友提供抓包需求素材
2021年08月23日 13点08分 5
能把几个软件官网的帮助文件抓下来吗?
2021年08月24日 00点08分
@chixun9999 需要登录会员吗?什么网站?
2021年08月24日 01点08分
暂时没遇到,遇到找你。
2021年08月24日 05点08分
2021年08月24日 08点08分
level 12
🌞Shawn 楼主
今日分享, 使用VBA+Selenium在豆瓣抓取指定话题的最新图片.
主要功能: API函数URLDownloadToFile下载文件, 获取图片集合, 获取图片链接
代码分享:
Option Explicit
'安装 SeleniumBasic 2.0.9
'配置当前版本的Chrome Driver
'Tools->Reference->Selenium Type Library->OK
'声明API函数
#If VBA7 Then
Private Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As LongPtr, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As LongPtr, _
ByVal lpfnCB As LongPtr) As LongPtr
#Else
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, _
ByVal szURL As String, _
ByVal szFileName As String, _
ByVal dwReserved As Long, _
ByVal lpfnCB As Long) As Long
#End If
Public cd As Selenium.ChromeDriver
Sub Douban_Selenium()
Dim url As String, row As Long
Dim strFileName As String, strPath As String, FullName As String
Dim clsIMG As Selenium.WebElements, img As Selenium.WebElement, link As String
url = "https://www.douban.com/gallery/topic/108090/?sort=new" '话题->镜头下的秋天
strPath = Environ$("USERPROFILE") & "\Downloads\" '下载路径
Set cd = New Selenium.ChromeDriver
With cd
.AddArgument "start-maximized" '窗口最大化
.Timeouts.PageLoad = 20000 '加载时长20秒
.Start
.Get url, Raise:=False '访问网址(忽略错误提示)
Set clsIMG = .FindElementsByClass("img-wrapper") '获取图片集合
For Each img In clsIMG
link = img.FindElementByTag("img").Attribute("src") '获取图片链接(默认为small size)
' link = Replace(link, "/s/", "/l/") '替换为large size链接
strFileName = Mid(link, InStrRev(link, "/") + 1) '截取文件名
FullName = strPath & strFileName '合成完整路径
URLDownloadToFile 0, link, FullName, 0, 0 '下载文件
Next img
.Quit '退出
End With
End Sub
2021年08月24日 07点08分 6
level 7
[真棒][真棒][真棒]
2021年08月24日 08点08分 8
level 12
楼主威武,一统江湖
2021年08月24日 10点08分 9
一起研究,共同进步
2021年08月24日 12点08分
level 12
🌞Shawn 楼主
今日分享, 使用VBA+Selenium在TED抓取视频的标题,作者,日期,以及超链接.
主要功能: 多页数循环, 获取视频集合, 跳过无标题的视频
代码分享:
Option Explicit
'安装 SeleniumBasic 2.0.9
'配置当前版本的Chrome Driver
'Tools->Reference->Selenium Type Library->OK
Public cd As Selenium.ChromeDriver
Sub TED_Selenium()
Dim url As String, row As Long
Dim cntPage As Integer, page As Integer
Dim clsMedia As Selenium.WebElements, media As Selenium.WebElement
Dim title As String, author As String, postedDate As String, link As String
url = "https://www.ted.com/talks?language=zh-cn&page="
Set cd = New Selenium.ChromeDriver
With cd
.AddArgument "start-maximized" '窗口最大化
.Timeouts.PageLoad = 10000 '加载时长10秒
.Start baseUrl:="https://www.ted.com"
.Get "/", Raise:=False '访问网址(忽略错误提示)
cntPage = 5 '指定页数
' cntPage = .FindElementByXPath("//*[@id='browse-results']/div[2]/div/a[5]").TextAsNumber '最大页数
row = 2
For page = 1 To cntPage
.Get url & page, Raise:=False '访问网址(忽略错误提示)
Set clsMedia = .FindElementsByClass("media__message") '获取视频集合
For Each media In clsMedia
If UBound(Split(media.Text, Chr(10))) > 1 Then '跳过无标题的视频
author = Split(media.Text, Chr(10))(0)
title = Split(media.Text, Chr(10))(1)
postedDate = Split(media.Text, Chr(10))(2)
postedDate = Mid(postedDate, InStr(postedDate, " ") + 1)
link = media.FindElementByTag("a").Attribute("href") '获取视频链接
'写入Excel
Cells(row, "A") = title
Cells(row, "B") = author
Cells(row, "C") = postedDate
Cells(row, "D").Hyperlinks.Add Cells(row, "D"), link
row = row + 1
End If
Next media
Next
.Quit '退出
End With
End Sub
2021年08月25日 05点08分 11
level 11

2021年08月27日 15点08分 13
level 12
🌞Shawn 楼主
pc端回帖提示APP扫一扫是什么操作?求助:有没有好用的移动端VB Editor?
2021年09月01日 01点09分 15
level 9
看看是啥
2021年09月01日 01点09分 16
level 1
能抓取试题答案吗[太开心]
2021年09月01日 07点09分 17
需要看网站具体的页面设置,具体问题具体分析
2021年09月01日 08点09分
level 12
🌞Shawn 楼主
安装SeleniumBasic,使用VBA自动完成浏览器中的操作
2021年11月10日 08点11分 18
[大拇指]
2022年05月05日 11点05分
level 9
这代码复制了可以直接用吗
2022年05月07日 07点05分 19
得装selenium,用Chrome好些
2022年05月11日 05点05分
@贴吧用户_Q12DaVK 好吧,还好安装其他东西啊,没弄过这种
2022年05月11日 10点05分
其实用python最好,不过这里是VBA吧
2022年05月12日 16点05分
@贴吧用户_Q12DaVK 我知道,python爬虫我已经学了,但是vba的不知道怎么弄
2022年05月13日 06点05分
1 2 尾页