Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从html标记对信息进行分类_Python_Html_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 从html标记对信息进行分类

Python 从html标记对信息进行分类,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,我的问题可能有一个更好的标题,但这里是: 我使用BeautifulSoup和findall将html中的元素返回到列表中,下面是我得到的示例: [<div class="tightLt col span-1-3"> <div class="middle"> <div class="cell"><i class="sqLed middle sm yellow margRtXs "></i></div>

我的问题可能有一个更好的标题,但这里是: 我使用BeautifulSoup和findall将html中的元素返回到列表中,下面是我得到的示例:

[<div class="tightLt col span-1-3">
    <div class="middle">
        <div class="cell"><i class="sqLed middle sm yellow margRtXs "></i></div>
        <div class="cell"><span class="middle">Neutral Outlook</span></div>
    </div>
</div>,
<div class="tightLt col span-1-3">
    <div class="middle">
        <div class="cell"><i class="sqLed middle sm yellow margRtXs "></i></div>
        <div class="cell"><span class="middle"><span class="showDesk">No opinion of</span> CEO</span>
        </div>
    </div>
</div>]
[<div class="tightLt col span-1-3">
    <div class="middle">
        <div class="cell"><i class="sqLed middle sm red margRtXs "></i></div>
        <div class="cell"><span class="middle">Doesn't Recommend</span></div>
    </div>
</div>,
<div class="tightLt col span-1-3">
    <div class="middle">
        <div class="cell"><i class="sqLed middle sm red margRtXs "></i></div>
        <div class="cell"><span class="middle">Negative Outlook</span></div>
    </div>
</div>,
<div class="tightLt col span-1-3">
    <div class="middle">
        <div class="cell"><i class="sqLed middle sm yellow margRtXs "></i></div>
        <div class="cell"><span class="middle"><span class="showDesk">No opinion of</span> CEO</span>
        </div>
    </div>
</div>]

使用BeautifulSoup扩展和优化的解决方案:

输出:

No opinion of CEO
No opinion of CEO
No opinion of CEO
No opinion of CEO
Approves of CEO
No opinion of CEO
Approves of CEO
Approves of CEO
Approves of CEO

您指的是“CEO批准”,它没有出现在提供的HTML中。我们是否应该假设您指的是第二列。如果你的问题能准确地包括你想说的话,那会很有帮助。一些开始使用的代码也会很有用。发布您当前的代码,html标记不足以对此表示抱歉,我的意思是“>No opinion of CEO”对应于第一个和第二个html列表中的CEO批准。我将发布完整的代码,但您需要先登录以获取汤中x的数据。选择(“[class='showDesk']”):打印(x.text)应该会给出与您想要的结果相同的结果。您是如何确定选择器的?@edyvedy13,经验,我的朋友。。。你能给我解释一下怎么做吗?因为我必须在不同的点处理相同的事情,因为当我右键单击并说“复制选择器”时,我没有得到same@edyvedy13,
div#employeerreviews li.empReview div.cell.reviewBodyCell span[class='showDesk']
是包含CEO标记的所需span元素的完整路径层次结构。这就是全部
from bs4 import BeautifulSoup
import requests

url = "https://www.glassdoor.com/Reviews/Walmart-Reviews-E715.htm"
html_content = requests.get(url, headers={'user-agent': 'Mozilla/5.0'}).content
soup = BeautifulSoup(html_content, "lxml")

selector = "div#EmployerReviews li.empReview div.cell.reviewBodyCell span[class='showDesk']"
for x in soup.select(selector):
    print(x.parent.text)
No opinion of CEO
No opinion of CEO
No opinion of CEO
No opinion of CEO
Approves of CEO
No opinion of CEO
Approves of CEO
Approves of CEO
Approves of CEO