フッククローラーの知識ポイントを引き出す
地図()
自動タスク割り当て、URL配列を順番に実行、マルチプロセスクローラーに便利
def scrape(url):
try:
urllib.request.urlopen(url)
print(f'URL {url} Scraped')
except (urllib.error.HTTPError, urllib.error.URLError):
print(f'URL {url} not Scraped')
if __name__ == '__main__':
pool = Pool(processes=3)
urls = [
'https://www.baidu.com',
'http://www.meituan.com/',
'http://blog.csdn.net/',
'http://xxxyxxx.net'
]
pool.map(scrape, urls)
pool.close()
リクエストのいくつかの用途
画像を取得する必要がある場合は、対応するストレージ形式で直接保存できます。r.contentはネイティブ(バイトスタイル)文字列であり、r.textはエンコードされたUnicodeを返すことに注意してください。
r = requests.get(CONST.RESOURCES[0], headers=headers)
# print(r.text)
with open("picTest.png",'wb') as pic:
pic.write(r.content)
r.cookiesは、Cookieを取得および設定してから、ヘッダーにCookieを設定できます。
'Cookie': '_octo=GH1.1.1849343058.1576602081; _ga=GA1.2.90460451.1576602
リクエストでCookieを個別に指定することもできます
jar = requests.cookies.RequestsCookieJar()
for cookie in cookies.split(';'):
key, value = cookie.split('=', 1)
jar.set(key, value)
r = requests.get('https://github.com/', cookies=jar, headers=headers)
セッションとSSL証明書
request.Session()確立されたセッション
SSL証明書検証エラー
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
検証済みのパラメータを追加することで解決
response = requests.get('url', verify=False)
タイムアウト設定
パラメータが追加されていない場合、デフォルトではタイムアウトはトリガーされません
r = requests.get('url', timeout=(5, 30))
認証
リクエストに付属のauthパラメータを使用します
r = requests.get('url', auth=('admin', 'admin'))
プロキシ設定
インターネット上で効果的なプロキシプールを探して、以下のIP
を私の現在の浅い知識に置き換えますが、IPの置き換えはあまり役に立たないようです、そして私はもっと学びます
proxies = {
'http': 'http://10.10.10.10:1080',
'https': 'http://10.10.10.10:1080',
}
requests.get('https://httpbin.org/get', proxies=proxies)