程序设置UA方法汇总:
User-Agent
访问地址
wget设置user-agent:
wget --user-agent='CCBot/2.0 (https://commoncrawl.org/faq/)' https://ip.useragentinfo.com
curl设置user-agent:
curl -v -I --user-agent 'CCBot/2.0 (https://commoncrawl.org/faq/)' https://ip.useragentinfo.com
kotlin httpclient设置user-agent:
val httpClient = HttpClients.custom().build(); val httpGet = HttpGet("https://ip.useragentinfo.com") httpGet.setHeader("User-Agent", "CCBot/2.0 (https://commoncrawl.org/faq/)") try { val response = httpClient.execute(httpGet) val entity = response.entity println(EntityUtils.toString(entity)) EntityUtils.consumeQuietly(entity) response.close() } catch (e: Exception) { httpGet.releaseConnection() }
java httpclient设置user-agent:
HttpClient httpClient = HttpClients.custom().build(); HttpGet httpGet = new HttpGet("https://ip.useragentinfo.com"); httpGet.setHeader("User-Agent", "CCBot/2.0 (https://commoncrawl.org/faq/)"); try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); // consume the response entity System.out.println(EntityUtils.toString(entity)); EntityUtils.consumeQuietly(entity); } catch (Exception e) { httpGet.releaseConnection(); }
kotlin URLConnection设置user-agent:
val url = URL("https://ip.useragentinfo.com") val connection = url.openConnection() connection.setRequestProperty("User-agent", "CCBot/2.0 (https://commoncrawl.org/faq/)"); val br = BufferedReader(InputStreamReader(connection.getInputStream())) val result = StringBuilder() var line: String? while (br.readLine().also { line = it } != null) { result.append(line) } println(result)
Python selenium设置user-agent:
Selenium文档
chromedriver阿里云镜像下载地址
from selenium import webdriver if __name__ == '__main__': chrome_driver_binary = "/usr/local/bin/chromedriver" #需根据chromedriver存放位置,修改改行代码。chromedeiver下载地址见上 chrome_option = webdriver.ChromeOptions() chrome_option.add_argument('--headless') # 16年之后chrome给出了headless解决方案,抢了PhantomJS饭碗 ua = "CCBot/2.0 (https://commoncrawl.org/faq/)" chrome_option.add_argument('user-agent="' + ua + '"') browser = webdriver.Chrome(chrome_driver_binary, options=chrome_option, ) browser.get("https://ip.useragentinfo.com") print(browser.title) browser.close()
Python requests设置user-agent:
requests官方网站
import requests headers = {'user-agent': 'CCBot/2.0 (https://commoncrawl.org/faq/)'} res = requests.get("https://ip.useragentinfo.com",headers=headers) print(res.text)
Python urllib设置user-agent:
Python urllib官方文档
import urllib.request opener = urllib.request.build_opener() opener.addheaders = [('User-agent', 'CCBot/2.0 (https://commoncrawl.org/faq/)')] with opener.open('https://ip.useragentinfo.com') as f: print(f.read().decode('utf-8'))执行上面的代码可能会报证书校验失败,错误内容如下:
urllib.error.URLError:<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)>
或者ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)
解决方法具体如下:
方法1:使用未经校验的ssl
import urllib.request import ssl ssl._create_default_https_context = ssl._create_unverified_context opener = urllib.request.build_opener() opener.addheaders = [('User-agent', 'CCBot/2.0 (https://commoncrawl.org/faq/)')] with opener.open('https://ip.useragentinfo.com') as f: print(f.read().decode('utf-8'))
方法2:修改为下面的代码
import urllib.request import ssl import certifi req = urllib.request.Request(url='https://ip.useragentinfo.com',headers={"User-Agent":"CCBot/2.0 (https://commoncrawl.org/faq/)"},method="GET") with urllib.request.urlopen(req, context=ssl.create_default_context(cafile=certifi.where())) as f: print(f.read().decode('utf-8'))
Python requests设置user-agent:
Scrapy官方文档
# -*- coding: utf-8 -*- import scrapy class ExampleSpider(scrapy.Spider): name = 'example' start_urls = ['https://ip.useragentinfo.com'] custom_settings = {"DEFAULT_REQUEST_HEADERS": {"User-Agent": "CCBot/2.0 (https://commoncrawl.org/faq/)"}} def parse(self, response): print(response.css('title').getall())
Node.js request设置user-agent:
request github地址
let request = require('request'); request({ url: 'https://ip.useragentinfo.com', headers:{ "User-Agent":"CCBot/2.0 (https://commoncrawl.org/faq/)" } }, function (error, response, body) { if (!error && response.statusCode == 200) { console.info(body); } });
Php Laravel中Guzzle设置user-agent:
Guzzle文档
$client = new \GuzzleHttp\Client(); $request = new \GuzzleHttp\Psr7\Request('get', 'https://ip.useragentinfo.com', ['User-Agent' => 'CCBot/2.0 (https://commoncrawl.org/faq/)']); $response = $client->send($request,['timeout' => 2]); echo($response->getStatusCode());
Android okhttp设置user-agent:(kotlin格式)
okhttp github地址
val request = Request.Builder().url("https://ip.useragentinfo.com").addHeader("User-Agent","CCBot/2.0 (https://commoncrawl.org/faq/)").build() try { okHttpClient.newCall(request).execute().use { response -> Log.i("test",response.body!!.string()) } } catch (e: Exception) { Log.i("test",e.message.toString()) }
Android Volley设置user-agent:(kotlin格式)
Volley github地址
val queue = Volley.newRequestQueue(this) val url = "https://ip.useragentinfo.com" val getRequest: StringRequest = object : StringRequest( Method.GET, url, Response.Listener { response -> // response Log.d("Response", response!!.reader().readText()) }, Response.ErrorListener { error -> // TODO Auto-generated method stub Log.d("ERROR", "error => $error") } ) { @Throws(AuthFailureError::class) override fun getHeaders(): Map<String, String> { val params: MutableMap<String, String> = HashMap() params["User-Agent"] = "CCBot/2.0 (https://commoncrawl.org/faq/)" return params } } queue.add(getRequest)
浏览器修改UA方法汇总
设置UA快捷导航