
初学 python,想尝试用 python 登陆 v2ex。 第一次尝试,使用登录信息登陆
import requests from bs4 import BeautifulSoup url = r"signin" v2ex_session = requests.Session() f = v2ex_session.get(url) soup = BeautifulSoup(f.content,"html.parser") Once= soup.find('input',{'name':'once'})['value'] user = {'u':'username', 'p':'password', 'once': once, 'next': '/'}#这里的 p 和 u 在浏览器上看是乱码,我想问一下,这个乱码是什么编码,会一直改变吗? v2ex_session.post(url, data = user) f = v2ex_session.get('http://www.v2ex.com/settings') print f.content 第二次尝试,使用 cookie,不贴代码了,最终也是失败了,是因为 https 的原因吗?可否使用 cookies 登陆 https 的链接呢?
最后希望告知登陆的正确姿势,万分感谢!!!
1 RLib 2017 年 7 月 30 日 那个乱码是随机生成的表单名,在登录前先获取 |
2 linw1995 2017 年 7 月 30 日 (⊙⊙)跟一楼一样发现了这个问题,换了表单名,可是还是不行。 |
3 giuem 2017 年 7 月 30 日 |
4 ranleng 2017 年 7 月 30 日 加 Header ? |
5 TKKONE PRO 提交表单的 user 和 password 的字段名是随机的 |
6 madfishy 2017 年 7 月 30 日 想采集,哈哈 |
7 deeeeeeep 2017 年 7 月 30 日 with requests.Session() as s: req = s.get(url) print("cookies="+repr(req.cookies)) html = req.text user = re.findall(r'input\s+type="text"\s+class="sl"\s+name="(\S+)"',html) key = re.findall(r'input\s+type="password"\s+class="sl"\s+name="(\S+)"',html) Once= re.findall(r'input\s+type="hidden"\s+value="(\S+)"\s+name="once"',html) postdata = {user[0]:"uuuuuu",key[0]:"123456","once":once[0],"next":"/"} print("data="+repr(postdata)) login_req = requests.post(url,headers=headers,data=postdata,cookies=req.cookies) |
8 coordinate OP |
9 numberwolf 2017 年 7 月 30 日 你们这样真的好吗... |
10 lieh222 2017 年 7 月 31 日 能不能偷摸着做,没准因为你一个帖子大家都得改 |