New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dynamics 函数失效 #1
Comments
为什么我用你的代码最后输出一个None |
@JasonWen1 你把最后一个函数的#号去掉看看页面的源代码
|
@JasonWen1 我以为是网站改版了,但刚刚我运行还是成功的,你能把完整的代码和输出po出来吗 |
@univerone 我也是运行了,结果是一个None, 4个网址的响应代码分别是如下: 我查了下 但我正常浏览器登陆github是OK的啊,而且测试的时候我浏览器github的帐号都是退出了的 |
@heavenkiller2018 我测试https://github.com/session时,响应代码也是422。请问你是怎么解决这个问题的呢? |
我的也是这个问题 |
请问如何解决HTTPError: 422 Client Error: Unprocessable Entity for url: https://github.com/session这个问题呢 |
遇到了一样的问题。 |
@ljtckkk 是的,应该是加了一些其他的验证。
|
我按照楼主说的修改完, 没有任何问题啊, 很奇怪 |
你的个人资料里面没有设置展示的name和email吧,我开始也是,改完之后就好了 |
xpath版本的: import requests, re
from lxml import etree
class Login(object):
def __init__(self):
self.headers = {
'Referer': 'https://github.com/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
'Host': 'github.com'
}
self.login_url = 'https://github.com/login'
self.post_url = 'https://github.com/session'
self.feed_url = 'https://github.com/dashboard-feed'
self.logined_url = 'https://github.com/settings/profile'
self.session = requests.Session()
def dynamics(self, html):
print('*'*10+'dynamicing'+'*'*10)
selector = etree.HTML(html)
# print("*"*20, etree.tostring(selector).decode('utf-8'))
# print(selector.xpath('//div[@class="d-flex flex-items-baseline"]'))
dynamics = selector.xpath('//div[@class="d-flex flex-items-baseline"]//div')
# print(dynamics)
for item in dynamics:
etree.strip_elements(item, 'span')
dynamic = ' '.join(item.xpath('.//text()')).replace('\n', ' ').strip()
dynamic = re.sub(' +', ' ', dynamic)
print(dynamic)
print('*' * 10 + 'dynamic end' + '*' * 10)
def profile(self, html):
print('*'*10+'profileing'+'*'*10)
selector = etree.HTML(html)
name = selector.xpath('//input[@id="user_profile_name"]/@value')[0]
email = selector.xpath('//select[@id="user_profile_email"]/option[@value!=""]/text()')[0]
print(name, email)
def token(self):
response = self.session.get(self.login_url, headers=self.headers)
selector = etree.HTML(response.text)
token = selector.xpath('//div//input[2]/@value')[0]
return token
def login(self, email, password):
post_data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': self.token(),
'login': email,
'password': password
}
response = self.session.post(self.post_url, data=post_data, headers=self.headers)
response = self.session.get(self.feed_url, headers=self.headers)
if response.status_code == 200:
self.dynamics(response.text)
# print('response.text', response.text)
response = self.session.get(self.logined_url, headers=self.headers)
if response.status_code == 200:
# print(response.text)
self.profile(response.text)
if __name__ == '__main__':
login = Login()
login.login(email='email', password='password') |
感谢!已经更新到 README。 |
还是不行呀,显示none,我也更改了资料了。还有什么叫:现在的github 关注人动态是通过ajax加载的(在原来的response里找不到)。能不能讲解一下 |
|
试了下脚本,不能直接用呀?有啥需要调整的么 requests.exceptions.ConnectionError: HTTPSConnectionPool(host='github.com', port=443): Max retries exceeded with url: /login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000002C7C3B986A0>: Failed to establish a new connection: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。',)) |
返回None的好兄弟们,你们有可能是没有关注人导致 dynamics = selector('div[class="d-flex flex-items-baseline"] div') 没有目标,其实已经登陆上了 ,在login()方法中,在最后加上print(response.text),打印出登陆后的页面后,在结果中按ctrl+F搜索自己的名字,会发现你的名字出现在结果中就登陆上了 |
楼上说的有道理,但既然前几章不都已经有解析AJAX的吗? |
pyquery和xpath这两个版本都能成功运行。 |
可能你的github没有关注人或者profile没有设置好。 |
None,还有一种情况,是触发了邮箱验证,我把response抓取下来打开就是email 设备验证码页面,需要去邮箱拿到验证码 |
邮件验证有422的情况,偶尔可以正常浏览。
|
邮箱验证不应该放在一开始的登录请求那吗 |
现在的github 关注人动态是通过ajax加载的(在原来的response里找不到),要向'https://github.com/dashboard-feed' 发送请求
这是我修改的代码,主要是dynamic这个函数以及增加的feed_url, 修改后运行结果和书上截图相同
The text was updated successfully, but these errors were encountered: