废品通过请求,CURL和BeautifulSoup形成wsj

时间:2017-07-07 07:57:52

标签: curl web-crawler python-requests

我是wsj的付费会员,我试图废弃文章来完成我的NLP项目。我以为我保持了会议。

rs = requests.session()
login_url="https://sso.accounts.dowjones.com/login?client=5hssEAdMy0mJTICnJNvC9TXEw3Va7jfO&protocol=oauth2&redirect_uri=https%3A%2F%2Faccounts.wsj.com%2Fauth%2Fsso%2Flogin&scope=openid%20idp_id&response_type=code&nonce=18091b1f-2c73-4a93-ab10-77b0d4d4f9d3&connection=DJldap&ui_locales=en-us-x-wsj-3&mg=prod%2Faccounts-wsj&state=NfljSw-Gz-TnT_I6kLjnTa2yxy8akTui#!/signin" 
payload={
    "username":"xxx@email",
    "password":"myPassword",
}
result = rs.post(
    login_url, 
    data = payload, 
    headers = dict(referer=login_url)
)

我要解析的文章。

r = rs.get('https://www.wsj.com/articles/singapore-prime-minister-lee-rejects-claims-he-misused-state-powers-in-family-feud-1499094761?tesla=y')

然后我发现html仍然是非会员的

我还尝试了另一种方法,使用CURL在我登录后保存cookie

curl -c cookies.txt -I "https://www.wsj.com"
curl -v cookies.txt "https://www.wsj.com/articles/singapore-prime-minister-lee-rejects-claims-he-misused-state-powers-in-family-feud-1499094761?tesla=y" > test.html

结果是一样的。

我不太熟悉浏览器后面的身份验证工作的机制。有人可以解释为什么上述方法都失败了,我应该如何解决它以实现我的目标。非常感谢你。

1 个答案:

答案 0 :(得分:0)

您的尝试失败,因为使用的协议是oauth2.0。这不是基本身份验证。

这里发生的事情是:

  • 在调用登录网址https://accounts.wsj.com/login时,服务器端会生成一些信息:connection& client_id
  • 在提交用户名/密码时,会调用网址https://sso.accounts.dowjones.com/usernamepassword/login,其中需要一些参数(上一个connection& client_id + oauth2的一些静态参数:scoperesponse_typeredirect_uri
  • 从上一次登录呼叫收到响应,该响应提供自动提交的表单。此表单有3个参数wawresultwctxwresultJWT)。此表单执行对https://sso.accounts.dowjones.com/login/callback的调用,以检索包含code=AjKK8g0pZZfvYpju
  • 等代码参数的网址
  • 调用URL https://accounts.wsj.com/auth/sso/login?code=AjKK8g0pZZfvYpju,使用有效的用户会话检索Cookie

使用curlgreppupjq的bash脚本:

username="user@gmail.com"
password="YourPassword"

login_url=$(curl -s -I "https://accounts.wsj.com/login")
connection=$(echo "$login_url" | grep -oP "Location:\s+.*connection=\K(\w+)")
client_id=$(echo "$login_url" | grep -oP "Location:\s+.*client_id=\K(\w+)")

#connection=$(echo "$login_url" | gawk 'match($0, /Location:\s+.*connection=(\w+)&/, data) {print data[1]}')
#client_id=$(echo "$login_url" | gawk 'match($0, /Location:\s+.*client_id=(\w+)&/, data) {print data[1]}')

rm -f cookies.txt

IFS='|' read -r wa wresult wctx < <(curl -s 'https://sso.accounts.dowjones.com/usernamepassword/login' \
      --data-urlencode "username=$username" \
      --data-urlencode "password=$password" \
      --data-urlencode "connection=$connection" \
      --data-urlencode "client_id=$client_id" \
      --data 'scope=openid+idp_id&tenant=sso&response_type=code&protocol=oauth2&redirect_uri=https%3A%2F%2Faccounts.wsj.com%2Fauth%2Fsso%2Flogin' | pup 'input json{}' | jq -r 'map(.value) | join("|")')

# replace double quote ""
wctx=$(echo "$wctx" | sed 's/&#34;/"/g')

code_url=$(curl -D - -s -c cookies.txt 'https://sso.accounts.dowjones.com/login/callback' \
     --data-urlencode "wa=$wa" \
     --data-urlencode "wresult=$wresult" \
     --data-urlencode "wctx=$wctx" | grep -oP "Location:\s+\K(\S*)")

curl -s -c cookies.txt "$code_url"

# here call your URL loading cookies.txt
curl -s -b cookies.txt "https://www.wsj.com/articles/singapore-prime-minister-lee-rejects-claims-he-misused-state-powers-in-family-feud-1499094761?tesla=y"