Python请求提供的页面文本与Internet Exp不同

2024-04-28 22:10:50 发布

您现在位置:Python中文网/ 问答频道 /正文

正在查看我的stackoverflow用户配置文件页:https://stackoverflow.com/users/2683104/roberto

该网站显示我已经成为会员316天(截图在帖子末尾)。如果我在我的浏览器(IE11)中view source,我可以看到这些数据来自days-visited类。在

但是如果我使用Python请求来寻找相同的days-visited信息,那么数据不会出现在任何地方。为什么?在

from requests import Session
from BeautifulSoup import BeautifulSoup

s = Session()

url = 'https://stackoverflow.com/users/2683104/roberto'
page = s.get(url)
soup = BeautifulSoup(page.text)
print soup.prettify() #server response, prettified

# following returns error
# AttributeError: 'NoneType' object has no attribute 'getText'
#days_visited = soup.find('span', attrs={'id':'days-visited'}).getText()

s.close()

屏幕截图

screenshot

查看源文件view_source

python请求python_requests


Tags: 数据fromhttpsimportcomurlsessionpage
2条回答

该字段对脚本(或其他用户)不可见。如果您想删除这段信息,您需要让您的脚本登录并存储适当的cookies。在

以下是非您的用户看到的: Profile

他们看到的代码块:

 <tbody>
            <tr>
                <th>visits</th>
                <td>member for</td>

                <td class="cool" title="2013-08-14 15:38:01Z">11 months</td>
            </tr>
            <tr>
                <th></th>
                <td>seen</td>

                <td class="supernova" title="2014-08-08 05:26:50Z">
                    <span title="2014-08-08 05:26:50Z" class="relativetime">6 mins ago</span>
                </td>
            </tr>
        </tbody>

通常,我建议不要为数据刮取堆栈溢出,而是使用API,但是这段特定的信息不会作为User对象的一部分返回。在

正如评论所说,“访问天数”只在您登录时显示。只有成员自己才能看到。在

您可以在浏览器中找到您的cookies,并在您的请求中使用cookies。在

http://docs.python-requests.org/en/latest/user/quickstart/#cookies

相关问题 更多 >