东京奥运会正在火热进行中,各国奥运健儿们都在努力拼搏,为自己的国家争取一枚奖牌。中国是体育大国,在每年的奥运会上奖牌数都名列前茅。从目前的冠军得奖人数看中国还是暂时领先的,在这次中国奥会员冠军中最让让大家关注的是,00年的杨倩居然还大学霸呀。这得让父母骄傲有这么一个奥运冠军加学霸女儿呀。
言归正传,作为一个爬虫工作者,我们今天分享下如何使用python查询每天最新的奥运会金牌排行榜。我们的数据来源可以从央视网官网获取。金牌排行榜:2020.cctv.com/medal_list/
我们可以通过数据连接获取具体的获取者信息,代码示例如下:
import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Demo
{
// 代理验证信息
final static String ProxyUser = "username";
final static String ProxyPass = "password";
// 代理服务器(产品官网 www.16yun.cn)
final static String ProxyHost = "t.16yun.cn";
final static Integer ProxyPort = 31111;
// 设置IP切换头
final static String ProxyHeadKey = "Proxy-Tunnel";
public static String getUrlProxyContent(String url)
{
Authenticator.setDefault(new Authenticator() {
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(ProxyUser, ProxyPass.toCharArray());
}
});
// 设置Proxy-Tunnel
Random random = new Random();
int tunnel = random.nextInt(10000);
String ProxyHeadVal = String.valueOf(tunnel);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ProxyHost, ProxyPort));
try
{
// 处理异常、其他参数
Document doc = Jsoup.connect(url).timeout(3000).header(ProxyHeadKey, ProxyHeadVal).proxy(proxy).get();
if(doc != null) {
System.out.println(doc.body().html());
}
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception
{
// 要访问的目标页面
String targetUrl = "https://2020.cctv.com/medal_list/";
getUrlProxyContent(targetUrl);
}
}
希望在接下来的各种比赛里,中国对继续加油!