批量抓取网页文件(图片)
本示例可参考示例文件"CaseStudy\WebViewer\网页自动化.Table"的窗口"抓取网页图片"。
本节只是用图片作为例子,任何其他类型文件的批量抓取,都可以复用本节的代码逻辑。
本节的任务是在抓取网页数据(爬虫)的基础上增加图书封面图片的抓取功能。
首先在开始位置增加一行代码,用于将下载目录设置为项目的附件目录:
wv.DefaultDownloadFolderPath = ProjectPath &
"Attachments"
抓取图片的代码为:
'
4. 获取封面图片
Dim
imgFile
As
String
=
""
Dim
imgEle = bookItem.querySelector(".image_container
img.thumbnail")
Dim
imgURL
As
String
= (Await
imgEle.src).Trim("""")
'获取封面图片链接
Dim
tmpFile
As
String
= FileSys.GetName(imgURL)
'获取封面图片文件名
wv.DownLoadFile(imgURL, tmpFile)
'开始下载文件
Dim
downop =
Await
wv.DownloadTaskAsync(600)
'每次下载等候10秒
If
downop
IsNot
Nothing
AndAlso
downop.State = CoreWebView2DownloadState.Completed
Then
imgFile = FileSys.GetName(downop.ResultFilePath)
'下载成功获得实际的文件名
End
If
提示:
1、这里用DownLoadFile函数下载图片,如果是其他类型的文件,且有下载按钮,建议模拟单击下载按钮
2、 如果DownLoadFile函数和模拟单击都失败,可以考虑用HttpClient下载
下面的是完整的参考代码:
'''Async
' 目标站点:books.toscrape.com
爬取前三页图书及其封面图片
Dim
wv
As
WebViewer = e.Form.Controls("WebViewer1").WebViewer
Dim
totalPage
As
Integer
= 3
'
定义需要爬取的总页数
DataTables("表A").DataRows.Clear()
'清除现有数据
wv.DefaultDownloadFolderPath =
ProjectPath &
"Attachments"
'设置默认下载目录为项目附件目录
' 循环遍历每一页
For
pageIndex
As
Integer
= 1
To
totalPage
'
拼接分页URL(Foxtable不支持插值,使用String.Format)
Dim
pageUrl
As
String
pageUrl =
String.Format("https://books.toscrape.com/catalogue/page-{0}.html",
pageIndex)
Dim request =
wv.CreateWebResourceRequest(pageUrl)
'异步加载
+ 超时控制(20秒)
Dim
navRet
As
Integer
=
Await
wv.NavigateWithWebResourceRequestAsync(request, 20)
If
navRet <> 0
Then
wv.Stop()
'停止加载
MessageBoxA.Show(String.Format("第{0}页加载失败,已经抓取{1}",
pageIndex, pageIndex - 1))
Exit
For
End
If
'获取图书集合(.product_pod
为单本书容器)
Dim
bookList = wv.querySelectorAll(".product_pod")
'
获取集合长度(Foxtable标准写法,不能For
Each)
Dim
bookCount
As
Integer
=
Await
bookList.length
'用下标循环遍历每一本图书
For
i
As
Integer
= 0
To
bookCount - 1
Dim
bookItem = bookList(i)
'
下标取值,禁止For
Each
'1.
获取图书名称
h3 > a
Dim nameEle =
bookItem.querySelector("h3
a")
If
Await
nameEle.IsNull()
Then
Continue
For
'
元素不存在则跳过
Dim
bookName
As
String
=
Await
nameEle.title
bookName =
bookName.Trim("""")
'
去除JS自带双引号
' 2.
获取价格
.price_color
Dim priceEle =
bookItem.querySelector(".price_color")
Dim
bookPrice
As
String
=
Await
priceEle.innerText
bookPrice =
bookPrice.Trim("""")
' 3.
获取库存状态
.instock
Dim stockEle =
bookItem.querySelector(".instock")
Dim
stockStatus
As
String
=
Await
stockEle.innerText
stockStatus =
stockStatus.Trim("""").Trim()
' 4.
获取封面图片
Dim
imgFile
As
String
=
""
Dim
imgEle = bookItem.querySelector(".image_container
img.thumbnail")
Dim
imgURL
As
String
= (Await
imgEle.src).Trim("""")
'获取封面图片链接
Dim
tmpFile
As
String
= FileSys.GetName(imgURL)
'获取封面图片文件名
wv.DownLoadFile(imgURL, tmpFile)
'开始下载文件
Dim
downop =
Await
wv.DownloadTaskAsync(10)
'每次下载等候10秒
If
downop
IsNot
Nothing
AndAlso
downop.State = CoreWebView2DownloadState.Completed
Then
imgFile = FileSys.GetName(downop.ResultFilePath)
'下载成功获得实际的文件名
End
If
' 5.写入到数据表
Dim
dr
As
DataRow = DataTables("表A").AddNew()
dr("书名")
= left(bookName, 64)
dr("价格")
= val (bookPrice.Trim("£"))
If
stockStatus =
"In
stock"
Then
dr("有货")
=
True
End
If
If
imgFile >
""
Then
dr("封面")
= imgFile
End
If
Next
'
模拟人工浏览延迟,降低反爬虫风险
Await
Task.Delay(1000)
Next
MessageBoxA.Show("前三页图书数据抓取完成!")