小姐姐網站(Python騷操作)

頭條有多火?很火,超級火!抖音有多火?比頭條有過之而無不及。據說抖音國際版在美國市場也被熱捧,可以用一句話概括:猶如蝗蟲所到之處,莊稼寸草不生。>> 無私分享Python爬蟲幹貨,如果你也想學習Python,@ 私信小編獲取很多人喜歡玩抖音,我也喜歡看抖音小姐姐,可如果拿著手機一條條去刷確實很耗時間,如果 Python 能幫忙篩選出顏值高的小姐姐那就省瞭很多事。現在如果掌握瞭Python,就可以自動下載這些高顏值小姐姐的抖音視頻,而且還都是高清無水印版本的呢!環境說明python 3.7.1centos 7.4pip 10.0.1部署[root@localhost ~]# python3.7 –versionPython 3.7.1[root@localhost ~]#[root@localhost ~]# pip3 install douyin如果安裝失敗,重新執行上面的命令即可,直到安裝完成。設置HTTP代理在學習Python爬蟲的時候,經常會遇見所要爬取的網站采取瞭反爬取技術,高強度、高效率地爬取網頁信息常常會給網站服務器帶來巨大壓力,所以同一個IP反復爬取同一個網頁,就很可能被封。這裡講述一個爬蟲技巧,設置極光代理IP。from bs4 import BeautifulSoupimport requestsimport random
def get_ip_list(url, headers):
web_data = requests.get(url, headers=headers)
soup = BeautifulSoup(web_data.text, 'lxml')
ips = soup.find_all('tr')
ip_list = []
for i in range(1, len(ips)):
ip_info = ips[i]
tds = ip_info.find_all('td')
ip_list.append(tds[1].text + ':' + tds[2].text)
return ip_list
def get_random_ip(ip_list):
proxy_list = []
for ip in ip_list:
proxy_list.append('http://' + ip)
proxy_ip = random.choice(proxy_list)
proxies = {'http': proxy_ip}
return proxies
if __name__ == '__main__':
url = 'http://h.jiguangdaili.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
}
ip_list = get_ip_list(url, headers=headers)
proxies = get_random_ip(ip_list)
print(proxies)運行上面的代碼會得到一個隨機的proxies,把它直接傳入requests的get方法中即可。web_data = requests.get(url, headers=headers, proxies=proxies)用代理IP不僅可以隱藏自身IP,還可以防止自身IP被封鎖。極光HTTP代理擁有海量IP,不僅使用方便快捷,更安全可靠。導入抖音模塊[root@localhost ~]# python3.7>>>import douyin>>>Python爬蟲代碼實現#!/usr/bin/env python 3
# -*- coding:utf-8 -*-
from bs4 import BeautifulSoup
from contextlib import closing
import requests, json, time, re, os, sys, time
import numpy as np
import csv
class ScrapyDouYin(object):
def __init__(self):
"""
抖音App視頻下載
"""
# SSL認證
pass
def get_video_urls(self, user_id):
"""
獲得視頻播放地址
Parameters:
nickname:查詢的用戶名
Returns:
video_names: 視頻名字列表
video_urls: 視頻鏈接列表
aweme_count: 喜歡的視頻數量
"""
video_names = []
video_urls = []
unique_id = ''
while unique_id != user_id:
search_url = 'https://api.amemv.com/aweme/v1/discover/search/?cursor=0&keyword=%s&count=10&type=1&retry_type=no_retry&iid=17900846586&device_id=34692364855&ac=wifi&channel=xiaomi&aid=1128&app_name=aweme&version_code=162&version_name=1.6.2&device_platform=android&ssmix=a&device_type=MI+5&device_brand=Xiaomi&os_api=24&os_version=7.0&uuid=861945034132187&openudid=dc451556fc0eeadb&manifest_version_code=162&resolution=1080*1920&dpi=480&update_version_code=1622' % user_id
req = requests.get(url=search_url, verify=False)
html = json.loads(req.text)
aweme_count = html['user_list'][0]['user_info']['favoriting_count']
uid = html['user_list'][0]['user_info']['uid']
nickname = html['user_list'][0]['user_info']['nickname']
unique_id = html['user_list'][0]['user_info']['unique_id']
user_url = 'https://www.douyin.com/aweme/v1/aweme/favorite/?user_id=%s&max_cursor=0&count=%s' % (uid, aweme_count)
req = requests.get(url=user_url, verify=False)
html = json.loads(req.text)
i = 1
user_id_lst = []
for each in html['aweme_list']:
share_desc = each['share_info']['share_desc']##視頻作者的抖音號
user_id_lst.append(each['author']['short_id'])
if '抖音-原創音樂短視頻社區' == share_desc:
video_names.append(str(i) + '.mp4')
i += 1
else:
video_names.append(share_desc + '.mp4')
video_urls.append(each['share_info']['share_url'])
user_id_lst = list(np.unique(user_id_lst))
return video_names, video_urls, nickname, user_id_lst
def get_download_url(self, video_url):
"""
獲得視頻播放地址
Parameters:
video_url:視頻播放地址
Returns:
download_url: 視頻下載地址
"""
req = requests.get(url=video_url, verify=False)
bf = BeautifulSoup(req.text, 'lxml')
script = bf.find_all('script')[-1]
video_url_js = re.findall('var data = \[(.+)\];', str(script))[0]
video_html = json.loads(video_url_js)
download_url = video_html['video']['play_addr']['url_list'][0]
return download_url
def video_downloader(self, video_url, video_name):
"""
視頻下載
Parameters:
None
Returns:
None
"""
size = 0
with closing(requests.get(video_url, stream=True, verify=False)) as response:
chunk_size = 1024
content_size = int(response.headers['content-length'])
if response.status_code == 200:
sys.stdout.write(' [文件大小]:%0.2f MB\n' % (content_size / chunk_size / 1024))
with open(video_name, "wb") as file:
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
size += len(data)
file.flush()
sys.stdout.write(' [下載進度]:%.2f%%' % float(size / content_size * 100))
sys.stdout.flush()
time.sleep(1)
def run(self):
"""
運行函數
Parameters:
None
Returns:
None
"""
self.hello()
# user_id = input('請輸入ID(例如173553803):')
user_id = 'lxylky1688'
video_names, video_urls, nickname, user_id_lst = self.get_video_urls(user_id)
if nickname not in os.listdir():
os.mkdir(nickname)
sys.stdout.write('視頻下載中:\n')
for num in range(len(video_urls)):
print(' %s\n' % video_urls[num])
video_url = self.get_download_url(video_urls[num])
if '\\' in video_names[num]:
video_name = video_names[num].replace('\\', '')
elif '/' in video_names[num]:
video_name = video_names[num].replace('/', '')
else:
video_name = video_names[num]
self.video_downloader(video_url, os.path.join(nickname, video_name))
print('')
csvFile = open('get_favorite_id.txt', 'w+')
try:
writer = csv.writer(csvFile)
writer.writerow(user_id_lst)
finally:
csvFile.close()
def hello(self):
"""
打印歡迎界面
Parameters:
None
Returns:
None
"""
print('*' * 100)
print('\t\t\t\t抖音App視頻下載小助手')
print('*' * 100)
if __name__ == '__main__':
Scrapydouyin = ScrapyDouYin()
Scrapydouyin.run()最後,成功獲取女神們的全部視頻…為瞭幫助大傢更輕松的學好Python,我給大傢分享一套Python學習資料,@ 私信小編領取PS:如果覺得本篇文章對您有所幫助,歡迎關註、收藏、轉發( ´・ω・)ノ(._.`)

本文出自快速备案,转载时请注明出处及相应链接。

本文永久链接: https://kuaisubeian.cc/48960.html

kuaisubeian