我已经不再使用Python和网络抓取了.我甚至无法实现抓取网站的第一步:登录.之前我尝试使用mechanize或者selenium,我想用requests第一.有人能帮我吗?
我一直试图登录的网站就在这里.
对于那些没有帐户并想要帮助我的人,我会提供使用Chrome获得的信息.
第一个POST:POST_1.JPG.第二个POST:POST_2.JPG
这是我的登录尝试代码:
import requests
s = requests.Session()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
payload = {'username': 'USERNAME',
'password': 'PASSWORD',
'submit.x': '21',
'submit.y': '12'}
s.post('https://ssologin.cuny.edu/oam/server/auth_cred_submit', data = payload, headers = headers)
target_page = s.get('https://home.cunyfirst.cuny.edu/psp/cnyepprd/EMPLOYEE/EMPL/h/?tab=DEFAULT', headers = headers)
if 'login' in target_page.url:
print("Failed to Login")
else:
print("Logged in Successfully!")
Run Code Online (Sandbox Code Playgroud)
谢谢!!!
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" class="pc chrome win psc_dir-ltr psc_form-xlarge" dir="ltr" lang="en">
<title>Some Title</title>
</html>
Run Code Online (Sandbox Code Playgroud)
如果我跑:
from lxml import etree
html = etree.parse('text.txt')
result = html.xpath('//title')
print(result)
Run Code Online (Sandbox Code Playgroud)
我会得到一个空列表.我想它与命名空间有关,但我无法弄清楚如何解决它.
示例:等待
<select id="myselect"></select>
Run Code Online (Sandbox Code Playgroud)
填充
<option value="123">One-two-three</option>
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 Python 中做到这一点?