【问题标题】:Webscraper Python BeautifulsoupWebscraper Python Beautifulsoup
【发布时间】:2022-01-20 05:35:32
【问题描述】:

我知道我想做的是最简单的,但它让我大吃一惊。我想使用 BeautifulSoup 从 HTML 页面 (https://partner.microsoft.com/en-us/membership/application-development-competency) 中提取数据。为此,我猜我需要使用 .find() 函数。不知道该做什么了。感谢每一种形式的帮助。 这是我正在使用的 HTML: [在此处输入图片描述][1] [1]:https://i.stack.imgur.com/sHAMF.png

import requests
from bs4 import BeautifulSoup 

url = 'https://partner.microsoft.com/en-us/membership/application-development-competency'
res = requests.get(url)
html_page = res.content
soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find("div",{"class":"col-md4[2]"})

output = ''
blacklist = [
    'style',
    'head',
    'meta',
    'col-md4[0]',
    'col-md4[1]',
]

for t in text:
    if t.parent.name not in blacklist:
        output += '{} '.format(t)

    sheet = '<html><body>' + text + '</body></html>';
    file_object  = open("record.html", "w+");
    file_object.write(sheet);
    file_object.close();

[在此处输入图片描述][1] [1]:https://i.stack.imgur.com/sHAMF.png

【问题讨论】:

  • 您必须使用 selenium 来单击下拉菜单,以便可以使用 js 加载数据,然后您可以从 selenium 获取 HTML 作为文本,然后您可以将此字符串解析为 HTML,然后您可以抓取它数据。

标签: python html python-3.x beautifulsoup python-requests


【解决方案1】:

你可以做这样的事情,但对于这个网站,我认为最好使用 XPath

import requests
from bs4 import BeautifulSoup


url = 'https://partner.microsoft.com/en-us/membership/application-development-competency'
response = requests.get(url)
soup = BeautifulSoup(response.text, features="lxml")
cols = soup.find_all("div", class_="col-md-4")
print(cols[6].getText())

【讨论】:

    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2015-10-28
    • 2020-04-14
    相关资源
    最近更新 更多