【问题标题】:Beautiful Soup Returning HTML full of Variables美丽的汤返回充满变量的 HTML
【发布时间】:2019-08-03 19:36:15
【问题描述】:

我创建了一个链接列表,这些链接连接到 LinkedIn 的页面。这些链接是使用 LinkedIn 的招聘功能(在付费墙后面)汇总的。当我将链接粘贴到浏览器中并检查 html 代码时,它看起来很标准,并且我能够轻松识别我正在搜索的元素(见下文)。

但是,当我运行我的 python 代码并使用漂亮的汤来获取 HTML 时,返回的 HTML 看起来与浏览器的检查元素视图中的外观完全不同。而不是普通的标签,HTML 充满了变量,基本上看起来不像我以前见过的任何东西(还没有做大量的抓取)。

是否有可能获得看起来像我在浏览器上看到的 HTML,而不是看起来很疯狂的东西?这些链接是使用 recuriter 搜索功能编译的,所以我想我以某种方式使用搜索变量而不是实际结果来提取 html,但我真的不知道。

其中一个网页链接:https://www.linkedin.com/recruiter/profile/168561385,W0U7,CAP?searchController=smartSearch&searchId=3392867616&pos=424&total=973&searchCacheKey=f4b1a865-50e8-4f59-ba48-9dff595e63e5%2CoUbi&searchRequestId=8322c8e0-4b99-4d99-b860-1bdef1653e8c%2CXsP1&searchSessionId=3392867616&origin=PAGE&memberAuth=168561385%2CW0U7%2CCAP

这是我用来创建 html 文件的代码。我希望最后一行提取我正在寻找的数据,假设我可以获得正确的 html。

#Used to create file
with open('departures.csv', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        browser.get(row['link'])
        page = BeautifulSoup(browser.page_source, 'lxml')
        html = page.prettify()
        with open("output1.html", "w") as file:
            file.write(unicode(html))

    #Code I want to Run right now it just returns an empty list
    position = page.find_all('span', class_= 'keyword')

当我使用浏览器转到链接时,我试图找到的 HTML:

<span class="keyword"> Account Manager</span>

Small Piece of Actual HTML returned: <code id="profile-data" style="display: none;">
<!--{"breadcrumbs":{"customSearchURL":"/recruiter/smartsearch? updateSearchHistory=false&decorateHits=true&decorateFacets=false&doFacetCounting=true&searchHistoryId=3392867616&resetFacets=false&searchCacheKey=f4b1a865-50e8-4f59-ba48-9dff595e63e5%2CoUbi&searchRequestId=4d25da0f-1f73-4722-8586-9652b3f98b97%2CQSZO&doResultCaching=false&forceResultFromCache=false&origin=PPSL&doProjectBasedCounting=false&count=25&start=700","linkContext":"Controller:smartSearch,Action:search,ID:3392867616","context":

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    LinkedIn 使用大量 JavaScript 来生成您在浏览器中看到的页面。开发者工具中的 DOM 元素检查器会显示该 JS 执行的当前结果,而不是浏览器下载的原始 HTML 页面。

    要在浏览器中查看实际的 HTML 页面源代码,请使用查看源代码(Ctrl+U 或 Command+U)。这应该显示类似于您在 Python 中获得的 HTML。

    如果您需要对最终生成的 DOM 输出进行一些抓取,您可能需要使用可以执行 JavaScript 的 headless browser,例如 Chrome controlled by Puppeteer

    【讨论】:

    • 有没有办法用 python/selenium 完成这个?我对javascript不是很熟悉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    相关资源
    最近更新 更多