Python爬虫学习(简单的模拟登陆(一))

本文介绍了一种使用Python的requests库配合lxml.etree进行网页内容抓取的方法,具体实现了从指定微博账号主页抓取微博内容的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以成功的爬取我室友的微博首页

#-*-coding:utf8-*-

import requests
from lxml import etree

cook = {"Cookie": "此处请填写你获取到的Cookie"}
url = 'http://weibo.cn/u/xxxxxxxx' #此处请修改为微博网址
# html = requests.get(url).content
# print html
html = requests.get(url, cookies = cook).content
# html = requests.get(url, cookies = cook).text

# html = bytes(bytearray(html, encoding='utf-8'))
selector = etree.HTML(html)
content = selector.xpath('//span[@class="ctt"]')
for each in content:
    text = each.xpath('string(.)')
    b = 1
    print text


模拟登录超星学习通,你需要先了解超星学习通的登录方式。超星学习通的登录方式有两种:账号密码登录和扫码登录。账号密码登录需要你模拟表单提交,扫码登录需要你模拟二维码的生成和扫描。 以下是账号密码登录的模拟登录步骤: 1.首先,引入requests库: ```python import requests ``` 2.获取登录页面的cookies和token: ```python login_url = 'https://passport2.chaoxing.com/login?fid=&newversion=true&refer=http://i.mooc.chaoxing.com' login_page = requests.get(login_url) cookies = login_page.cookies token = re.findall(r'name="lt" value="(.*?)"', login_page.text)[0] ``` 3.构造登录请求的表单数据: ```python data = { 'username': 'your_username', 'password': 'your_password', 'lt': token, 'rememberMe': 'false' } ``` 4.构造登录请求头: ```python headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3', 'Referer': 'https://passport2.chaoxing.com/login?fid=&newversion=true&refer=http://i.mooc.chaoxing.com', 'Host': 'passport2.chaoxing.com', 'Origin': 'https://passport2.chaoxing.com', } ``` 5.发送登录请求: ```python login_req = requests.post(login_url, data=data, headers=headers, cookies=cookies) ``` 6.检查登录是否成功: ```python if login_req.text.find('登录成功') != -1: print('登录成功') else: print('登录失败') ``` 以上就是模拟登录超星学习通的基本流程。但需要注意的是,登录页面的token可能会定期更换,需要根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值