Gia*_* F. 2 python cookies selenium instagram selenium-chromedriver
我正在尝试通过HTTP请求(感谢Python的“ requests”模块)向Instagram发出登录请求,然后将结果移植到Selenium的chromedriver,以使用户已经登录。我正在使用“ requestium ”模块尝试一下。当我通过Session成功登录到网站内部时,似乎阻止了我切换到驱动程序以拥有已登录页面的权限。这是控制台打印我的错误
DevTools listening on ws://127.0.0.1:12779/devtools/browser/73b726a1-520f-4d08-8982-a9ada65ead2b
Traceback (most recent call last):
File "requestiumTest.py", line 124, in <module>
s.transfer_session_cookies_to_driver() # You can maintain the session if needed
File "C:\Users\gforcell\AppData\Local\Continuum\anaconda3\lib\site-packages\requestium\requestium.py", line 114, in transfer_session_cookies_to_driver
'expiry': c.expires, 'domain': c.domain})
File "C:\Users\gforcell\AppData\Local\Continuum\anaconda3\lib\site-packages\requestium\requestium.py", line 235, in ensure_add_cookie
self.add_cookie(cookie)
File "C:\Users\gforcell\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 872, in add_cookie
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})
File "C:\Users\gforcell\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\gforcell\AppData\Local\Continuum\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unable to set cookie
(Session info: chrome=66.0.3359.139)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.15063 x86_64)
Run Code Online (Sandbox Code Playgroud)
虽然这是复制它的代码
from requestium import Session, Keys
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
from seleniumrequests import Chrome
from os.path import join
import platform
import json
import hashlib
import hmac
import six.moves.urllib as urllib
def findPathForDriver():
lookfor = "chromedriver.exe" if platform.system() == "Windows" else "chromedriver"
placeWhereToStart = "C:\\" if platform.system() == "Windows" else "/"
for root, dirs, files in os.walk(placeWhereToStart):
if lookfor in files:
with open('path.txt', 'w') as f:
f.write(join(root, lookfor))
return join(root, lookfor)
def generate_signature(data, IG_SIG_KEY, SIG_KEY_VERSION):
body = hmac.new(IG_SIG_KEY.encode('utf-8'), data.encode('utf-8'),hashlib.sha256).hexdigest() + '.' + urllib.parse.quote(data)
signature = 'ig_sig_key_version={sig_key}&signed_body={body}'
return signature.format(sig_key=SIG_KEY_VERSION, body=body)
def generate_device_id(seed):
volatile_seed = "12345"
m = hashlib.md5()
m.update(seed.encode('utf-8') + volatile_seed.encode('utf-8'))
return 'android-' + m.hexdigest()[:16]
def generate_UUID(uuid_type):
import uuid
generated_uuid = str(uuid.uuid4())
if uuid_type:
return generated_uuid
else:
return generated_uuid.replace('-', '')
def get_seed(*args):
m = hashlib.md5()
m.update(b''.join([arg.encode('utf-8') for arg in args]))
return m.hexdigest()
def send_request(endpoint, post=None, login=False, with_signature=True):
global last_response
global last_json
global s
s.headers.update({
'Connection': 'close',
'Accept': '*/*',
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie2': '$Version=1',
'Accept-Language': 'en-US',
'User-Agent': USER_AGENT
})
try:
if post is not None: # POST
if with_signature:
post = generate_signature(post, IG_SIG_KEY, SIG_KEY_VERSION)
response = s.post('https://i.instagram.com/api/v1/' + endpoint, data=post)
else: # GET
response = s.get('https://i.instagram.com/api/v1/' + endpoint)
except Exception as e:
print("Eccezione per colpa dell'endpoint " + endpoint)
return False
if response.status_code == 200:
last_response = response
last_json = json.loads(response.text)
return True
else:
return False
DEVICE_SETTINTS = {
'manufacturer': 'samsung',
'model': 'herolte',
'device': 'SM-G960F',
'android_version': 26,
'android_release': '8.0'
}
#41.0.0.13.92
USER_AGENT = 'Instagram 10.26.0 Android ({android_version}/{android_release}; 640dpi; 1440x2560; {manufacturer}; {device}; {model}; samsungexynos8890; en_US)'.format(
**DEVICE_SETTINTS)
IG_SIG_KEY = '4f8732eb9ba7d1c8e8897a75d6474d4eb3f5279137431b2aafb71fafe2abe178'
EXPERIMENTS = 'ig_android_disk_cache_match_journal_size_to_cache_max_count,ig_android_ad_move_carousel_indicator_to_ufi_universe,ig_android_universe_video_production,ig_android_live_follow_from_comments_universe,ig_android_ad_watchandinstall_universe,ig_android_live_analytics,ig_android_video_captions_universe,ig_android_offline_location_feed,ig_android_ontact_invite_universe,ig_android_insta_video_reconnect_viewers,ig_android_live_broadcast_blacklist,ig_android_checkbox_instead_of_button_as_follow_affordance_universe,ig_android_ufi_redesign_video_social_context,ig_android_stories_surface_universe,ig_android_verified_comments_universe,ig_android_preload_media_ahead_in_current_reel,android_instagram_prefetch_suggestions_universe,ig_android_direct_inbox_tray_suggested_user_universe,ig_android_direct_blue_tab,ig_android_light_status_bar_universe,ig_android_asset_button_new_content_animation,ig_android_async_network_tweak_universe,ig_android_react_native_lazy_modules_killswitch,ig_android_instavideo_remove_nux_comments,ig_video_copyright_whitelist,ig_android_ad_sponsor_label_story_top_design_universe,ig_android_business_action,ig_android_direct_link_style,ig_android_live_heart_enhancements_universe,ig_android_preload_item_count_in_reel_viewer_buffer,ig_android_auto_retry_post_mode,ig_android_fix_render_thread_crash,ig_android_shopping,ig_fbns_preload_default,ig_android_gesture_dismiss_reel_viewer,ig_android_ad_logger_funnel_logging_universe,ig_android_direct_links,ig_android_links_receivers,ig_android_ad_impression_backtest,ig_android_offline_freshness_toast_10_12,ig_android_invites_without_token_universe,ig_android_immersive_viewer,ig_android_mqtt_skywalker,ig_fbns_push,ig_android_react_native_universe,ig_android_special_brush,ig_android_live_consumption_abr,ig_android_story_viewer_social_context,ig_android_explore_verified_badges_stories_universe,ig_android_video_loopcount_int,ig_android_enable_main_feed_reel_tray_preloading,ig_android_ad_watchbrowse_universe,ig_android_react_native_ota,ig_android_discover_people_icon_in_others_profile,ig_android_log_mediacodec_info,ig_android_enable_back_navigation_nux_universe,ig_android_cold_start_feed_request,ig_video_use_sve_universe,ig_android_offline_explore_10_14,ig_android_stories_teach_gallery_location,ig_android_http_stack_experiment_2017,ig_android_stories_device_tilt,ig_android_pending_request_search_bar,ig_android_fb_topsearch_sgp_fork_request,ig_android_animation_perf_reporter_timeout,ig_android_new_block_flow,ig_android_direct_address_links,ig_android_share_profile_photo_to_feed_universe,ig_android_stories_private_likes,ig_android_text_background,ig_android_stories_video_prefetch_kb,ig_android_su_activity_feed,ig_android_live_stop_broadcast_on_404,ig_android_render_iframe_interval,ig_android_boomerang_entry,ig_android_camera_shortcut_universe,ig_android_fetch_fresh_viewer_list,ig_android_ad_media_url_logging_universe,ig_android_phone_confirm_rate_limit_language_universe,ig_android_keep_http_cache_on_user_switch,ig_android_facebook_twitter_profile_photos,ig_android_full_user_detail_endpoint,ig_android_direct_sqlite_universe,ig_android_family_bridge_share,ig_android_search,ig_android_insta_video_consumption_titles,ig_android_live_notification_control,ig_android_camera_universe,ig_android_instavideo_audio_only_mode,ig_android_live_video_reactions_consumption_universe,ig_android_swipe_fragment_container,ig_creation_growth_holdout,ig_android_live_save_to_camera_roll_universe,ig_android_ad_cta_redesign_universe,ig_android_sticker_region_tracking,ig_android_unified_inbox,ig_android_offline_main_feed_10_11,ig_android_chaining_teaser_animation,ig_android_business_conversion_value_prop_v2,ig_android_redirect_to_low_latency_universe,ig_android_feed_header_profile_ring_universe,ig_family_bridges_holdout_universe,ig_android_following_follower_social_context,ig_android_video_keep_screen_on,ig_android_profile_photo_as_media,ig_android_insta_video_consumption_infra,ig_android_sms_consent_in_edit_profile,ig_android_infinite_scrolling_launch,ig_in_feed_commenting,ig_android_live_broadcast_enable_year_class_2011,ig_android_direct_phone_number_links,ig_android_direct_share_sheet_ring,ig_android_stories_weblink_creation,ig_android_histogram_reporter,ig_android_network_cancellation,ig_android_react_native_insights,ig_android_insta_video_audio_encoder,ig_android_family_bridge_bookmarks,ig_android_dash_for_vod_universe,ig_android_direct_mutually_exclusive_experiment_universe,ig_android_stories_selfie_sticker,ig_android_ad_add_per_event_counter_to_logging_event,ig_android_rtl,ig_android_direct_send_auto_retry,ig_android_direct_video_autoplay_scroll,ig_android_promote_from_profile_button,ig_android_share_spinner,ig_android_profile_share_username,ig_android_sidecar_edit_screen_universe,ig_promotions_unit_in_insights_landing_page,ig_android_save_longpress_tooltip,ig_android_constrain_image_size_universe,ig_android_business_new_graphql_endpoint_universe,ig_ranking_following,ig_android_universe_reel_video_production,ig_android_sfplt,ig_android_offline_hashtag_feed,ig_android_live_skin_smooth,ig_android_stories_posting_offline_ui,ig_android_direct_add_local_thread_in_inbox,ig_android_swipe_navigation_x_angle_universe,ig_android_offline_mode_holdout,ig_android_non_square_first,ig_android_insta_video_drawing,ig_android_react_native_usertag,ig_android_swipeablefilters_universe,ig_android_analytics_logger_running_background_universe,ig_android_save_all,ig_android_reel_viewer_data_buffer_size,ig_android_disk_cache_has_sanity_check,ig_direct_quality_holdout_universe,ig_android_family_bridge_discover,ig_android_react_native_restart_after_error_universe,ig_story_tray_peek_content_universe,ig_android_profile,ig_android_high_res_upload_2,ig_android_http_service_same_thread,ig_android_remove_followers_universe,ig_android_skip_video_render,ig_android_live_viewer_comment_prompt_universe,ig_android_search_client_matching,ig_explore_netego,ig_android_boomerang_feed_attribution,ig_android_explore_story_sfslt_universe,ig_android_rendering_controls,ig_android_os_version_blocking,ig_android_encoder_width_safe_multiple_16,ig_android_direct_video_autoplay,ig_android_snippets_profile_nux,ig_android_e2e_optimization_universe,ig_android_disk_usage,ig_android_save_collections,ig_android_live_see_fewer_videos_like_this_universe,ig_android_live_view_profile_from_comments_universe,ig_formats_and_feedbacks_holdout_universe,ig_fbns_blocked,ig_android_instavideo_periodic_notif,ig_android_empty_feed_redesign,ig_android_marauder_update_frequency,ig_android_suggest_password_reset_on_oneclick_login,ig_android_live_special_codec_size_list,ig_android_enable_share_to_messenger,ig_android_live_video_reactions_creation_universe,ig_android_live_hide_viewer_nux,ig_android_channels_home,ig_android_sidecar_gallery_universe,ig_android_live_using_webrtc,ig_android_insta_video_broadcaster_infra_perf,ig_android_business_conversion_social_context,android_ig_fbns_kill_switch,ig_android_retry_story_seen_state,ig_android_react_native_universe_kill_switch,ig_android_stories_book_universe,ig_android_all_videoplayback_persisting_sound,ig_android_cache_layer_bytes_threshold,ig_android_comment_deep_linking_v1,ig_android_business_promotion,ig_android_anrwatchdog,ig_android_qp_kill_switch,ig_android_ad_always_send_ad_attribution_id_universe,ig_android_2fac,ig_direct_bypass_group_size_limit_universe,ig_android_promote_simplified_flow,ig_android_share_to_whatsapp,ig_fbns_dump_ids,ig_android_ad_show_mai_cta_loading_state_universe,ig_android_skywalker_live_event_start_end,ig_android_toplive_verified_badges_universe,ig_android_live_join_comment_ui_change,ig_android_draw_button_new_tool_animation,ig_video_max_duration_qe_preuniverse,ig_android_http_stack_kz_debug,ig_request_cache_layer,ig_android_carousel_feed_indicators_universe,ig_android_new_optic,ig_android_mark_reel_seen_on_Swipe_forward,ig_fbns_shared,ig_android_capture_slowmo_mode,ig_android_save_multi_select,ig_android_mead,ig_android_video_single_surface,ig_android_offline_reel_feed,ig_android_video_download_logging,ig_android_last_edits,ig_android_exoplayer_4142,ig_android_snippets_haptic_feedback,ig_android_gl_drawing_marks_after_undo_backing,ig_android_mark_seen_state_on_viewed_impression,ig_android_live_backgrounded_reminder_universe,ig_android_disable_comment_public_test,ig_android_user_detail_endpoint,ig_android_comment_tweaks_universe,ig_android_add_to_last_post,ig_save_insights,ig_android_live_enhanced_end_screen_universe,ig_android_ad_add_counter_to_logging_event,ig_android_sidecar,ig_android_direct_split_new_message_button,ig_android_grid_video_icon,ig_android_ad_watchandlead_universe,ig_android_progressive_jpeg,ig_android_offline_story_stickers,ig_android_direct_inbox_unseen_hint,ig_android_top_live_titles_universe,ig_android_video_prefetch_for_connectivity_type,ig_android_ad_holdout_16m5_universe,ig_android_sync_on_background_enhanced,ig_android_upload_reliability_use_fbupload_lib,ig_android_samsung_app_badging,ig_android_offline_commenting,ig_android_insta_video_abr_resize,ig_android_insta_video_sound_always_on,ig_android_disable_comment'
SIG_KEY_VERSION = '4'
path = findPathForDriver()
s = Session(webdriver_path=path,
browser='chrome',
default_timeout=15)
username = <insert_username>
password = <insert_password>
device_id = generate_device_id(get_seed(username, password))
uuid = generate_UUID(True)
last_response = None
last_json = None
if (send_request('si/fetch_headers/?challenge_type=signup&guid=' + generate_UUID(False), login=True)):
data = json.dumps({
'phone_id': generate_UUID(True),
'_csrftoken': last_response.cookies['csrftoken'],
'username': username,
'guid': uuid,
'device_id': device_id,
'password': password,
'login_attempt_count': '0',
})
if send_request('accounts/login/', data, True):
print("Tutto ok")
is_logged_in = True
user_id = last_json["logged_in_user"]["pk"]
rank_token = "{}_{}".format(user_id, uuid)
print(rank_token)
ranktoken = rank_token
token = last_response.cookies["csrftoken"]
s.transfer_session_cookies_to_driver() # You can maintain the session if needed
s.driver.get('http://www.instagram.com')
Run Code Online (Sandbox Code Playgroud)
据我所知,Chrome似乎不喜欢以前生成的cookie,尽管它们看起来根本没有损坏。浏览器显示的错误是“请求太多”
小智 5
我在使用请求和硒时遇到了相同的错误。您需要先在域上获得一个页面,然后才能设置Cookie
driver.get(login_url)
for cookie in s.cookies:
driver.add_cookie({
'name': cookie.name,
'value': cookie.value,
'domain': cookie.domain
})
driver.get(dashboard_url)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4335 次 |
最近记录: |