通過爬蟲獲取失蹤兒童信息_風聞
小白学大数据-2021-07-13 16:35
一個人,一輛摩托車,車後插着一面尋子旗幟,很多人通過電影《失孤》認識了郭剛堂。二十多年來,他一直在鍥而不捨地做一件事——尋找被拐走的兒子郭新振。7月13日,公安部在北京召開發佈會,介紹電影《失孤》原型拐賣案件偵破情況:電影中的被拐兒童原型郭新振已被公安機關找到,犯罪嫌疑人被警方抓獲。看了整個事件的來龍去脈真的很感動,父母的愛真的很偉大。
關於失蹤兒童在我們國家依舊是一個很值得關注的羣體,那麼今天我們就通過爬蟲來獲取我們國家失蹤兒童的數據,希望可以更好的幫助那些失蹤的孩子早點回到父母的身邊。
首先我們使用的是Selenium
1、獲取http://bbs.baobeihuijia.com/forum-191-1.html這個版塊上的所有分頁頁面鏈接
2、設置代理, 代理我們可以通過www.16yun.cn獲取
3代碼信息如下:
from selenium import webdriver import string import zipfile
# 代理服務器(產品官網 www.16yun.cn) proxyHost = “t.16yun.cn” proxyPort = “31111”
# 代理驗證信息 proxyUser = “16OKTFDX” proxyPass = “940952”
def create_proxy_auth_extension(proxy_host, proxy_port, proxy_username, proxy_password, scheme=‘http’, plugin_path=None): if plugin_path is None: plugin_path = r’D:/{}_{}@t.16yun.zip’.format(proxy_username, proxy_password)
manifest_json = """ { “version”: “1.0.0”, “manifest_version”: 2, “name”: “16YUN Proxy”, “permissions”: [ “proxy”, “tabs”, “unlimitedStorage”, “storage”, “”, “webRequest”, “webRequestBlocking” ], “background”: { “scripts”: [“background.js”] }, “minimum_chrome_version”:“22.0.0” } """
background_js = string.Template( """ var config = { mode: “fixed_servers”, rules: { singleProxy: { scheme: “${scheme}”, host: “${host}”, port: parseInt(${port}) }, bypassList: [“foobar.com”] } };
chrome.proxy.settings.set({value: config, scope: “regular”}, function() {});
function callbackFn(details) { return { authCredentials: { username: “${username}”, password: “${password}” } }; }
chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: [""]}, [‘blocking’] ); """ ).substitute( host=proxy_host, port=proxy_port, username=proxy_username, password=proxy_password, scheme=scheme, )
with zipfile.ZipFile(plugin_path, ‘w’) as zp: zp.writestr(“manifest.json”, manifest_json) zp.writestr(“background.js”, background_js)
return plugin_path
proxy_auth_plugin_path = create_proxy_auth_extension( proxy_host=proxyHost, proxy_port=proxyPort, proxy_username=proxyUser, proxy_password=proxyPass)
option = webdriver.ChromeOptions()
option.add_argument("--start-maximized")
# 如報錯 chrome-extensions # option.add_argument("--disable-extensions")
option.add_extension(proxy_auth_plugin_path)
# 關閉webdriver的一些標誌 # option.add_experimental_option(’excludeSwitches’, [’enable-automation’])
driver = webdriver.Chrome(chrome_options=option)
# 修改webdriver get屬性 # script = ’’’ # Object.defineProperty(navigator, ‘webdriver’, { # get: () => undefined # }) # ’’’ # driver.execute_cdp_cmd(“Page.addScriptToEvaluateOnNewDocument”, {“source”: script})
driver.get(“http://bbs.baobeihuijia.com/forum-191-1.html”)貼出代碼的初衷是供大家學習爬蟲,大家只是研究下網絡框架即可,關於更多的爬蟲知識大家可以積極交流學習。