首页 > 解决方案 > 当提供的数据是 XML 时,如何从检查菜单中检索相同的 HTML?

问题描述

我正在使用 Python 的request模块来抓取这个网站:http ://reports.ieso.ca/public/Adequacy2/PUB_Adequacy2_20200114.xml

import requests

def get_info(date=None):
    headers = {
        "Content-Type": "text/html"
    }

    response = requests.get('http://reports.ieso.ca/public/Adequacy2/PUB_Adequacy2_20200114.xml', headers=headers,verify=False)
    print(response.text)
    return response

get_info()

现在它返回 XML,我理解。但是当我检查那个网站时,我看到的 HTML 结构是不同的,而且它的结构要好得多。

有没有办法通过请求而不是 XML 数据来获取该数据?还是其他替代方案?

标签: pythonweb-scrapingpython-requests

解决方案


我认为美丽的汤可能会满足您的要求。

装美汤

pip3 install beautifulsoup4

“汤”对象有望解析为您所期望的

import requests
from bs4 import BeautifulSoup

URL = 'https://www.monster.com/jobs/search/?q=Software-Developer&where=Australia'
page = requests.get(URL)

soup = BeautifulSoup(page.content, 'html.parser')

推荐阅读