Cooki模拟登陆(人人网)

我们在爬取网上一些数据时,必须登陆才能爬取到数据,这是我们就需要Cookie了,Cookie简单说就是服务器返回给我们的一些数据,保存到客户端,下次登陆时,服务器会识别这些数据,可以返回我们上次的数据:
我爬取的是人人网

在这里插入图片描述

  1. 当没有设置Cookie时,不论我们爬取那个网页的数据,都只能爬取到登陆页面,这里我就不演示了,在请求的头里不要设置Cookie就行了
  2. 代码
from urllib import request
dapeng_url = 'http://www.renren.com/880151247/profile'
headers = {
    'User-Agent':' Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
                 ' AppleWebKit/537.36 (KHTML, like Gecko) '
                 'Chrome/73.0.3683.86 Safari/537.36',
    'Cookie':'anonymid=jts9ysu5-tuyp2f; depovince=GW; _r01_=1; '
             'JSESSIONID=abc3iYNu61jbQ6s6oddNw; ick_login=b6b0b9b'
             '8-c14d-472e-9ad3-4221df783996; ick=aebabe76-c548-4d'
             '53-ab1b-c856846d8ec7; jebecookies=ed6dc536-d393-4c53'
             '-bd9a-2596a808ae6c|||||; _de=B1BBBAB916360F72F4627FFE'
             '19B3821C; p=92189c5bd1b14cdfb0cb26d422c3474d6; first_'
             'login_flag=1; ln_uact=15955034252; ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; '
             't=57b623a00434e5710fe7c2856c2338096; societyguester=57b623a00434e5710fe7c2856c2338096; '
             'id=970227406; xnsid=3cf0bd31; ver=7.0; loginfrom=null;'
             ' jebe_key=c6ac91f6-47b4-45d3-a1ab-bc163cb9c20a%7C95fdfd6f154b6ddbfbe4a74cb1'
             '12a907%7C1553755754281%7C1%7C1553755754060; wp_fold=0'
}

resp = request.Request(dapeng_url,headers=headers);
respon = request.urlopen(resp)
with open('renren.html','w',encoding='utf-8') as fp:
    fp.write(respon.read().decode('utf-8'))
  1. 结果,在文件目录就会出现下面的文件

在这里插入图片描述
右击,在浏览器中打开
在这里插入图片描述
爬取的结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java模拟登陆的示例代码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.List; import java.util.Map; public class LoginDemo { public static void main(String[] args) throws Exception { String loginUrl = "http://example.com/login"; String username = "your_username"; String password = "your_password"; // Create a new URL object URL url = new URL(loginUrl); // Create a new HttpURLConnection object HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Set the request method to POST connection.setRequestMethod("POST"); // Set the request headers connection.setRequestProperty("User-Agent", "Mozilla/5.0"); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // Set the request parameters String params = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8"); connection.setDoOutput(true); connection.getOutputStream().write(params.getBytes("UTF-8")); // Send the request int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // Read the response BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // Print the response System.out.println(response.toString()); // Get the cookies from the response headers Map<String, List<String>> headerFields = connection.getHeaderFields(); List<String> cookiesHeader = headerFields.get("Set-Cookie"); if (cookiesHeader != null) { // Parse the cookies and store them in a CookieManager object java.net.CookieManager cookieManager = new java.net.CookieManager(); for (String cookie : cookiesHeader) { cookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0)); } // Use the CookieManager to send subsequent requests with the cookies URL newUrl = new URL("http://example.com/protected_page"); HttpURLConnection newConnection = (HttpURLConnection) newUrl.openConnection(); cookieManager.getCookieStore().getCookies().forEach(c -> newConnection.addRequestProperty("Cookie", c.toString())); int newResponseCode = newConnection.getResponseCode(); if (newResponseCode == HttpURLConnection.HTTP_OK) { // Read the response BufferedReader newIn = new BufferedReader(new InputStreamReader(newConnection.getInputStream())); String newInputLine; StringBuffer newResponse = new StringBuffer(); while ((newInputLine = newIn.readLine()) != null) { newResponse.append(newInputLine); } newIn.close(); // Print the response System.out.println(newResponse.toString()); } else { System.out.println("Failed to fetch protected page"); } } } else { System.out.println("Failed to login"); } } } ``` 此代码将使用POST请求发送用户名和密码到登录页面。如果登录成功,它将获取响应标头中的cookie,并将其存储在CookieManager对象中。然后,它将使用CookieManager对象向受保护的页面发送新请求,并在请求中添加所有cookie。如果此请求成功,它将打印受保护页面的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值