import re
import requests
import hashlib
import time
def get_index(url):
respose = requests.get(url)
if respose.status_code==200:
return respose.text
def parse_index(res):
urls = re.findall(r'class="items".?href="(.?)"', res,re.S) # re.S 把文本信息转换成1行匹配
return urls
def get_detail(urls):
for url in urls:
if not url.startswith('http'):
url='http://www.xiaohuar.com%s' %url
result = requests.get(url)
if result.status_code==200 :
mp4_url_list = re.findall(r'id="media".?src="(.?)"', result.text, re.S)
if mp4_url_list:
mp4_url=mp4_url_list[0]
print(mp4_url)
# save(mp4_url)
def save(url):
video = requests.get(url)
if video.status_code==200:
m=hashlib.md5()
m.updata(url.encode('utf-8'))
m.updata(str(time.time()).encode('utf-8'))
filename=r'%s.mp4'% m.hexdigest()
filepath=r'D:\%s'%filename
with open(filepath, 'wb') as f:
f.write(video.content)
def main():
for i in range(5):
res1 = get_index('http://www.xiaohuar.com/list-3-%s.html'% i )
res2 = parse_index(res1)
get_detail(res2)
if name == 'main':
main()
我运行之后也成功了,但是谁能告诉我这个我爬下来的数据放到哪个文件下了?
D:\文件名.mp4
D根目录下是吗?D根目录下没有。。。
@你说我记: 哈希算法把名称搞乱了,你看到的应该是:D:\e52662b0b8160765ebe93eb1a8115fc3.mp4 类似这种的