【问题标题】:Cannot webscrape elements with Playwright and BeautifulSoup无法使用 Playwright 和 BeautifulSoup 抓取元素
【发布时间】:2021-09-21 16:04:49
【问题描述】:

随着网站 (https://covid19.gov.vn/) 的更新,我正在尝试更新我的网络抓取脚本,但我终生无法找到如何解析 these elements。检查元素,它似乎像往常一样在那里,但我无法用 BeautifulSoup 解析它。我最初的尝试包括使用 Playwright 并再次尝试,但我仍然无法正确抓取它。查看源代码几乎就像元素根本不存在一样。任何对 HTML 和网页抓取有更多了解的人都可以向我解释这是如何工作的吗?我几乎被困在这里

这基本上是我放弃查看页面源之前的最后一次尝试:

from bs4 import BeautifulSoup as bs
import requests
from playwright.sync_api import sync_playwright


with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://covid19.gov.vn/")
    page_content = page.content()
    soup = bs(page_content, features="lxml")
    test = soup.findAll('div', class_ = "content-tab show", id="vi")
    print(test)
    browser.close()

我的想法是抓取并遍历里面的所有内容。但是,它不起作用。如果有人可以帮助我,将不胜感激!谢谢!

【问题讨论】:

    标签: python python-3.x beautifulsoup playwright-python


    【解决方案1】:

    试试下面的代码 - 它基于 HTTP GET 调用来获取您正在寻找的数据。

    import requests
    
    r = requests.get('https://static.pipezero.com/covid/data.json')
    if r.status_code == 200:
        data = r.json()
        print(data['total']['internal'])
    

    输出

    {'death': 17545, 'treating': 27876, 'cases': 707436, 'recovered': 475343}
    

    【讨论】:

    • 谢谢!我不知道存在这样的东西。非常感激! o_0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2017-05-10
    • 2021-10-24
    相关资源
    最近更新 更多