.NET从优酷专辑中采集所有视频及信息(VB.NET代码)
程序员文章站
2024-03-08 12:26:46
这个方法就是提取优酷的专辑id,然后一个个id进行循环采集网页代码,从中提取title标签和vid,没什么技术含量。=..= 采集中应用.net中的httpwebreque...
这个方法就是提取优酷的专辑id,然后一个个id进行循环采集网页代码,从中提取title标签和vid,没什么技术含量。=..=
采集中应用.net中的httpwebrequest和httpwebresponse类,代码分析用了正则表达式。
这个代码效率不是很好,一个网页的解析时间在0.5~2秒之间,不适合大量采集。也许将它转换成javascript速度会快一点吧。
暂时就研究这么多,代码直接发出来给大家共享一下。
代码vb.net,新建一个窗体frmmain,添加一个textbox,一个listbox,两个button,复制下面的代码:
imports system.net
imports system.io
imports system.text
imports system.text.regularexpressions
public class frmmain
structure vlist
dim id as integer
dim title as string
dim vid1 as string
dim vid2 as string
overloads function tostring() as string
return string.format("{0}:<{1}> [{2}]", id, title, vid1)
end function
end structure
dim mylist as new list(of vlist)
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
' 防止重复创建变量
dim wr1 as httpwebrequest
dim wr2 as httpwebresponse
dim ret as string
dim reg as match
dim g as group
dim previd as string = "" '上一个vid
dim nowid as integer = 0 '当前的视频集数
dim listurl as string = textbox1.text '获取专辑url,如 http://www.youku.com/playlist_show/id_2350764.html
dim tarurl as string = "http://v.youku.com/v_playlist/f{0}" '{0}listid
reg = regex.match(listurl, "playlist_show/id_(\d+).*\.html")
if not reg.success then
msgbox("专辑列表提取失败!")
exit sub
end if
g = reg.groups(1)
tarurl = string.format(tarurl, g.value) & "o{1}p{0}.html" '{0}集数 {1}排序
wr1 = httpwebrequest.create(textbox1.text)
wr2 = wr1.getresponse
ret = new streamreader(wr2.getresponsestream, encoding.getencoding(wr2.characterset)).readtoend
reg = regex.match(ret, "<title>(.+) - 专辑 - 优酷视频</title>")
if not reg.success then
msgbox("专辑名称提取失败!")
else
g = reg.groups(1)
msgbox("专辑名:《" & g.value & "》")
end if
do
' 从web流中获取页面文本
wr1 = httpwebrequest.create(string.format(tarurl, nowid, "0")) '按倒序方式查找视频
wr2 = wr1.getresponse
ret = new streamreader(wr2.getresponsestream, encoding.getencoding(wr2.characterset)).readtoend
'textbox2.text = ret
' 创建一个临时视频列表变量
dim nlist as new vlist
nlist.id = nowid '获取id
' 获取videoid
reg = regex.match(ret, "var\s+videoid\s*=\s*""(\d+)""\s*;")
if not reg.success then exit do
g = reg.groups(1)
' 如果vid等于上一个vid最退出
if g.value = previd then exit do
nlist.vid1 = g.value
' 获取videoid2
reg = regex.match(ret, "var\s+videoid2\s*=\s*""((\w|=)+)""\s*;") '"var\s+videoid2\s*=\s*""(\w+)""\s*;")
if not reg.success then exit do
g = reg.groups(1)
nlist.vid2 = g.value
' 获取标题
reg = regex.match(ret, "<title>(.+) - (.+) - 视频 - 优酷视频 - 在线观看 - </title>")
if not reg.success then
nlist.title = "{名称查找错误}"
else
g = reg.groups(2)
nlist.title = g.value
end if
' 收尾工作
mylist.add(nlist) '添加到总列表中
previd = nlist.vid1 '记录最后一个vid
wr2.close()
me.text = nowid & " : 处理完成!"
nowid += 1
loop
wr2.close()
msgbox(nowid & " 个视频全部采集处理完成!")
button2_click(sender, e)
end sub
private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
listbox1.items.clear()
for each ls as vlist in mylist
listbox1.items.add(string.format("{0}:<{1}> [{2}]", ls.id, ls.title, ls.vid1))
next
mylist.clear()
end sub
end class
夜闻香原创
博客: http://clso.cnblogs.com
主页: http://cleclso.cn
qq:315514678 e-mail:clso#qq.com
欢迎技术交流!
采集中应用.net中的httpwebrequest和httpwebresponse类,代码分析用了正则表达式。
这个代码效率不是很好,一个网页的解析时间在0.5~2秒之间,不适合大量采集。也许将它转换成javascript速度会快一点吧。
暂时就研究这么多,代码直接发出来给大家共享一下。
代码vb.net,新建一个窗体frmmain,添加一个textbox,一个listbox,两个button,复制下面的代码:
复制代码 代码如下:
imports system.net
imports system.io
imports system.text
imports system.text.regularexpressions
public class frmmain
structure vlist
dim id as integer
dim title as string
dim vid1 as string
dim vid2 as string
overloads function tostring() as string
return string.format("{0}:<{1}> [{2}]", id, title, vid1)
end function
end structure
dim mylist as new list(of vlist)
private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click
' 防止重复创建变量
dim wr1 as httpwebrequest
dim wr2 as httpwebresponse
dim ret as string
dim reg as match
dim g as group
dim previd as string = "" '上一个vid
dim nowid as integer = 0 '当前的视频集数
dim listurl as string = textbox1.text '获取专辑url,如 http://www.youku.com/playlist_show/id_2350764.html
dim tarurl as string = "http://v.youku.com/v_playlist/f{0}" '{0}listid
reg = regex.match(listurl, "playlist_show/id_(\d+).*\.html")
if not reg.success then
msgbox("专辑列表提取失败!")
exit sub
end if
g = reg.groups(1)
tarurl = string.format(tarurl, g.value) & "o{1}p{0}.html" '{0}集数 {1}排序
wr1 = httpwebrequest.create(textbox1.text)
wr2 = wr1.getresponse
ret = new streamreader(wr2.getresponsestream, encoding.getencoding(wr2.characterset)).readtoend
reg = regex.match(ret, "<title>(.+) - 专辑 - 优酷视频</title>")
if not reg.success then
msgbox("专辑名称提取失败!")
else
g = reg.groups(1)
msgbox("专辑名:《" & g.value & "》")
end if
do
' 从web流中获取页面文本
wr1 = httpwebrequest.create(string.format(tarurl, nowid, "0")) '按倒序方式查找视频
wr2 = wr1.getresponse
ret = new streamreader(wr2.getresponsestream, encoding.getencoding(wr2.characterset)).readtoend
'textbox2.text = ret
' 创建一个临时视频列表变量
dim nlist as new vlist
nlist.id = nowid '获取id
' 获取videoid
reg = regex.match(ret, "var\s+videoid\s*=\s*""(\d+)""\s*;")
if not reg.success then exit do
g = reg.groups(1)
' 如果vid等于上一个vid最退出
if g.value = previd then exit do
nlist.vid1 = g.value
' 获取videoid2
reg = regex.match(ret, "var\s+videoid2\s*=\s*""((\w|=)+)""\s*;") '"var\s+videoid2\s*=\s*""(\w+)""\s*;")
if not reg.success then exit do
g = reg.groups(1)
nlist.vid2 = g.value
' 获取标题
reg = regex.match(ret, "<title>(.+) - (.+) - 视频 - 优酷视频 - 在线观看 - </title>")
if not reg.success then
nlist.title = "{名称查找错误}"
else
g = reg.groups(2)
nlist.title = g.value
end if
' 收尾工作
mylist.add(nlist) '添加到总列表中
previd = nlist.vid1 '记录最后一个vid
wr2.close()
me.text = nowid & " : 处理完成!"
nowid += 1
loop
wr2.close()
msgbox(nowid & " 个视频全部采集处理完成!")
button2_click(sender, e)
end sub
private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
listbox1.items.clear()
for each ls as vlist in mylist
listbox1.items.add(string.format("{0}:<{1}> [{2}]", ls.id, ls.title, ls.vid1))
next
mylist.clear()
end sub
end class
夜闻香原创
博客: http://clso.cnblogs.com
主页: http://cleclso.cn
qq:315514678 e-mail:clso#qq.com
欢迎技术交流!
下一篇: java 串口通信详细及简单实例