爬取uputoo视频
程序员文章站
2022-04-11 17:14:00
...
爬取uputoo视频
import json
import urllib.request
import os,time
from math import ceil
from lxml import etree
import re
import random
def get_heders():
ua_list = [
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0",
"Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16",
"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 SE 2.X MetaSr 1.0",
]
#随机抽取
import random
ua = random.choice(ua_list)
headers = {
'User-Agent':ua,
}
return headers
def handle_request(url,page):
url = url.format(page)
headers = get_heders()
request = urllib.request.Request(url=url,headers=headers)
return request
def parse_resp(response):
tree = etree.HTML(response)
href_list = tree.xpath('//div[@class="mask"]/a/@href')
return href_list
def main():
dirname = "video"
url = "http://wallpaper.upupoo.com/store/browVi/1-0-0-{}.htm"
for page in range(3,10):
print("正在下载%s" % page)
request = handle_request(url,page)
response = urllib.request.urlopen(request).read().decode('utf-8')
href_list = parse_resp(response)
for hrefnot in href_list:
href = "http://wallpaper.upupoo.com/" + hrefnot
headers = get_heders()
request = urllib.request.Request(url=href,headers=headers)
content = urllib.request.urlopen(request).read().decode('utf-8')
pat = re.compile('.*?var video = loadVideo.(.*?.mp4).*')
ret = pat.findall(content)[0].strip("'")
filename = dirname + ret.split('/')[-1]
try:
urllib.request.urlretrieve(ret,filename)
time.sleep(1)
print("结束%s" % page)
except Exception as e:
print('继续吓一跳')
if __name__ == '__main__':
main()