2017-07-28 80 views
0

我有这个网址:https://www.ft.com/content/87d644fc-73a4-11e7-aca6-c6bd07df1a3c登录和刮像ft.com网站与BeautifulSoup

它对应于需要注册的文章。我注册并可以在浏览器中看到内容。但是,当我使用上面的网址代码:

soup = BeautifulSoup(urllib2.urlopen(url), 'lxml') 
with open('ctp_output.txt', 'w') as f: 
    for tag in soup.find_all('p'): 
     f.write(tag.text.encode('utf-8') + '\n') 

尤其是,它在注册页面上重定向我。刮刮时有没有办法登录来访问文章?

回答

0

以下是基本知识。

转到登录页面。如果您使用Chrome浏览器,则可以将鼠标放在电子邮件输入区域上,然后使用上下文菜单(在Windows中),然后使用其“检查”条目来显示将用于提交电子邮件地址的form元素。它看起来像这样。

<form name="enter-email-form" action="/login/submitEmail" class="js-email-lookup-form" method="POST" data-test-id="enter-email-form" novalidate="true"> 
     <input type="hidden" name="location" value="https://www.ft.com/content/87d644fc-73a4-11e7-aca6-c6bd07df1a3c"> 
     <input type="hidden" name="continueUrl" value=""> 
     <input type="hidden" name="readerId" value=""> 
     <input type="hidden" name="loginUrl" value="/login?location=https%3A%2F%2Fwww.ft.com%2Fcontent%2F87d644fc-73a4-11e7-aca6-c6bd07df1a3c"> 
     <div class="lgn-box__title"> 
      <h1 class="lgn-heading--alpha">Sign in</h1> 
     </div> 
     <div class="o-forms-group"> 
      <label for="email" class="o-forms-label">Email address</label> 
      <input type="email" id="email" class="o-forms-text js-email" name="email" maxlength="64" autocomplete="off" autofocus="" required=""> 
      <input type="password" id="password" name="password" style="display:none"> 
      <label for="password"> 
     </label></div> 
     <div class="o-forms-group"> 
      <button class="o-buttons o-buttons--standout o-buttons--big" type="submit" name="Next">Next</button> 
     </div> 
    </form> 

您将需要从form元素收集action属性和所有input报表的名称 - 值对。您可以在requests library的POST请求中使用它们。

您可以为您的电子邮件地址和密码输入一次。然后,您应该可以通过请求发布URL的GET。

我必须警告你,我没有真正尝试过这个特定的网站。

+0

好的,谢谢,我会尽力,并让你张贴! – ben

+0

如果你这样做,那么我们可以改变你的问题的标题,例如登录到ft.com,以便其他人可以从你的经验中受益。 –

+1

绝对是!有效! – ben