使用 Python 和 Selenium 擷取 Twitter 追隨者
擷取數據範例
{
"userId": "1710236730010349568",
"isBlueVerified": false,
"following": false,
"canDm": false,
"canMediaTag": true,
"createdAt": "Fri Oct 06 10:13:15 +0000 2023",
"defaultProfile": true,
"defaultProfileImage": true,
"description": "",
"fastFollowersCount": 0,
"favouritesCount": 456,
"followersCount": 64,
"friendCount": 7320,
"hasCustomTimelines": false,
"isTranslator": false,
"listedCount": 0,
"location": "",
"mediaCount": 0,
"name": "Paislie Dimitrov",
"normalFollowersCount": 64,
"pinnedTweetIdsStr": [],
"possiblySensitive": false,
"profileImageUrlHttps": "https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png",
"profileInterstitialType": "",
"username": "PaisliDimit",
"statusesCount": 0,
"translatorType": "none",
"verified": false,
"wantRetweets": false,
"withheldInCountries": []
}
無需設置直接運行代碼
我們的指南提供完整的即用代碼以無縫擷取 Twitter 追隨者數據。使用 Python 和 Selenium,自動化數據收集並有效捕捉性能日誌,無需額外設置即可解鎖 Twitter 的洞察!
步驟 1:設置環境
首先,安裝 Selenium,這將允許我們自動化瀏覽器操作:
pip install -r requirements.txt
步驟 2:下載 ChromeDriver
您可以從這裡找到相應的 ChromeDriver 下載 ChromeDriver
步驟 3:設置 Chrome 選項
self.options = webdriver.ChromeOptions()
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
self.options.add_argument(f'user-agent={user_agent}')
self.options.add_argument('--disable-gpu')
self.options.add_argument('--no-sandbox')
self.options.add_argument('--disable-dev-shm-usage')
self.options.add_argument(f"--remote-debugging-port={remote_debugging_port}")
js_script_name = modify_random_canvas_js()
self.browser = self.get_browser(script_files=[js_script_name], record_network_log=True, headless=True)
步驟 4:訪問目標頁面
self.browser.switch_to.new_window('tab')
url= 'https://x.com/1_usd_promotion/verified_followers'
self.browser.get(url=url)
time.sleep(2)
exist_entry_id = []
self.get_network(exist_entry_id, result_list)
print(f'tweet result length = {len(result_list)}')
步驟 5:獲取瀏覽器性能日誌
performance_log = self.browser.get_log("performance")
for packet in performance_log:
msg = packet.get("message")
message = json.loads(packet.get("message")).get("message")
packet_method = message.get("method")
if "Network" in packet_method and 'Following' in msg:
request_id = message.get("params").get("requestId")
resp = self.browser.execute_cdp_cmd('Network.getResponseBody', {'requestId': request_id})
步驟 6:從回應中提取數據
body = resp.get("body")
body = json.loads(body)
instructions = body['data']['user']['result']['timeline']['timeline'].get('instructions', None)
if not instructions:
continue
for instruction in instructions:
entries = instruction.get('entries', None)
步驟 7:重要注意事項
- 登錄 Twitter 然後獲取 auth_token。了解如何獲取 auth token
- 您可以使用來自 Apify 的 API
- 您可以從 GitHub 獲取完整代碼
- 加入我們的討論小組! 點此
常見問題:FAQ
- Q: 什麼是網頁擷取?
網頁擷取就像使用特殊工具自動從網站收集信息。想像一個機器人幫助從頁面上收集數據,這樣您就不必手動進行。在這種情況下,我們專注於使用 Python 和 Selenium 擷取 Twitter 數據。
- Q: 如何開始擷取 Twitter 數據?
要開始擷取 Twitter 數據,您首先需要設置您的計算機。這包括安裝名為 Selenium 的軟件,它幫助您控制網頁瀏覽器。然後,您下載 ChromeDriver,這是一個允許 Selenium 與 Google Chrome 一起工作的輔助工具。
- Q: 什麼是 ChromeDriver,為什麼我需要它?
ChromeDriver 就像 Selenium 和 Google Chrome 的翻譯器。它幫助 Selenium 理解如何與 Chrome 瀏覽器互動。您需要它,以便 Selenium 可以自動化點擊按鈕或在 Twitter 上輸入信息等操作。
- Q: 擷取中的性能日誌是什麼?
性能日誌就像記錄您網頁擷取過程中所有事情的日記。它記錄您擷取工具(Selenium)和 Twitter 頁面之間的所有數據交換,幫助您了解您的程序正在發送什麼請求。
- Q: 擷取 Twitter 前我應考慮什麼?
在擷取 Twitter 之前,您需要登錄您的 Twitter 帳戶並獲取一個稱為 auth_token 的東西,以證明您有權訪問 Twitter 的數據。此外,要小心遵循 Twitter 的規則,以免被封鎖。
- Q: 如何避免在擷取時被封鎖?
為了避免被封鎖,請確保在請求之間引入延遲,旋轉代理,並避免在短時間內對 Twitter 的伺服器施加過多請求。