Web scraping 使用BeautifulSoup登录并抓取类似ft.com的网站

Web scraping 使用BeautifulSoup登录并抓取类似ft.com的网站,web-scraping,beautifulsoup,Web Scraping,Beautifulsoup,我有这个网址: 它对应于一篇需要注册的文章。我已注册,可以在浏览器中查看内容。但是,当我将此代码与上面的url一起使用时: 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') 特别是,它在注册页面上重定

我有这个网址:

它对应于一篇需要注册的文章。我已注册,可以在浏览器中查看内容。但是,当我将此代码与上面的url一起使用时:

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')
特别是,它在注册页面上重定向我。有什么方法可以在刮纸时登录以访问文章吗?

以下是一些基本信息

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

<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>

登录
电子邮件地址
下一个
您需要从
表单
元素中收集
操作
属性,并从
输入
语句中收集所有名称-值对。您可以在具有的POST请求中使用这些

您只需为您的电子邮件地址和密码执行一次。然后,您应该能够通过请求为URL发出GET

我必须提醒你,我实际上还没有在那个特定的网站上尝试过这个方法。

以下是一些基本知识

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

<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>

登录
电子邮件地址
下一个
您需要从
表单
元素中收集
操作
属性,并从
输入
语句中收集所有名称-值对。您可以在具有的POST请求中使用这些

您只需为您的电子邮件地址和密码执行一次。然后,您应该能够通过请求为URL发出GET


我必须警告你,我实际上还没有在那个特定的网站上尝试过这个方法。

如果你想用BeautifulSoup删除一个网站,我建议你使用这个库。它是一个非常轻量级的层,位于BeautifulSoup(解析HTML)和请求(获取页面)之上,但它将为您处理一些事情,如正确填写表单(即,您在此处需要的内容),遵循相关链接

MechanicalSoup也受到限制,因为它不解释JavaScript代码,因此无法在依赖JavaScript的网站上工作,但与直接使用BeautifulSoup和urllib或请求相比,它减少了手动操作


(注意:我是MechanicalSoup的作者之一)

如果你想用BeautifulSoup删除一个网站,我建议你使用这个库。它是一个非常轻量级的层,位于BeautifulSoup(解析HTML)和请求(获取页面)之上,但它将为您处理一些事情,如正确填写表单(即,您在此处需要的内容),遵循相关链接

MechanicalSoup也受到限制,因为它不解释JavaScript代码,因此无法在依赖JavaScript的网站上工作,但与直接使用BeautifulSoup和urllib或请求相比,它减少了手动操作


(注意:我是MechanicalSoup的作者之一)

如果您这样做,我们可以将您问题的标题更改为(比如)登录ft.com,以便其他人可以从您的经验中受益。如果您这样做,我们可以将您问题的标题更改为(比如)登录ft.com,以便其他人可以从您的经验中受益。