Python 如何选择具有特定属性类型的标记

Python 如何选择具有特定属性类型的标记,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,事情是这样的 我只想在其他凌乱的html中抓取这些标记 <table bgcolor="FFFFFF" border="0" cellpadding="5" cellspacing="0" align="center"> <tr> <td> <a href="./index.html?id=subjective&page=2"> <img src='htt

事情是这样的

我只想在其他凌乱的html中抓取这些标记

<table bgcolor="FFFFFF" border="0" cellpadding="5" cellspacing="0" align="center">
    <tr>
        <td>
            <a href="./index.html?id=subjective&page=2">
                <img src='https://www.dogdrip.net/?module=file&act=procFileDownload&file_srl=224868098&sid=cc8c0afbb679bef6420500988a756054&module_srl=78' style='max-width:180px;max-height:270px' align='absmiddle' title="cutie cat">
            </a>
        </td>
    </tr>
</table>
但是
soup.select('selector')
不起作用。它输出空列表。 我不知道为什么

第二,我试着用tag 我想爬的每一个都有特定的风格 所以我试着:

soup.select('img[style = fixedstyle]')
但这不管用。这将是语法错误

我只想爬 href链接列表 和img标题列表


请帮助我

如果
img
标记具有特定的样式值,您可以使用您尝试过的样式,只需删除额外的空格即可:

from bs4 import BeautifulSoup

html='''
<a href='link'>
    <img src='address' style='max-width:222px;max-height:222px' title='owntitle'>
</a>
<a href='link'>
    <img src='address1' style='max-width:222px;max-height:222px' title='owntitle1'>
</a>
<a href='link'>
    <img src='address2' style='max-width:222px;max-height:222px' title='owntitle2'>
</a>
'''

srcs=[]
titles=[]
soup=BeautifulSoup(html,'html.parser')
for img in soup.select('img["style=max-width:222px;max-height:222px"]'):
    srcs.append(img['src'])
    titles.append(img['title'])
print(srcs)
print(titles)

soup.select('img[style=fixedstyle]')
应该可以。删除固定样式为“最大宽度:222px”的额外空间;最大高度:222px’如果没有空格,它现在无法工作。我可能写错了什么。。。。里面太多了。所以决定因素只是风格。无论如何,谢谢你们的帮助,我在主要帖子的回复中写道,固定样式是“最大宽度:222px;最大高度:222px',且无法使用select[]soupsive.util.SelectorSyntaxerError:位置3处的属性选择器格式错误第1行:返回了img[style=max width:222px;最大高度:222px]错误。这就是我想解释的。很抱歉英文不好,你可以发布你想废弃的部分真正的html吗?我编辑了主帖。这些代码是实际的html代码
from bs4 import BeautifulSoup

html='''
<a href='link'>
    <img src='address' style='max-width:222px;max-height:222px' title='owntitle'>
</a>
<a href='link'>
    <img src='address1' style='max-width:222px;max-height:222px' title='owntitle1'>
</a>
<a href='link'>
    <img src='address2' style='max-width:222px;max-height:222px' title='owntitle2'>
</a>
'''

srcs=[]
titles=[]
soup=BeautifulSoup(html,'html.parser')
for img in soup.select('img["style=max-width:222px;max-height:222px"]'):
    srcs.append(img['src'])
    titles.append(img['title'])
print(srcs)
print(titles)
for a in soup.select('a'):
    srcs.append(a.select_one('img')['src'])
    titles.append(a.select_one('img')['title'])
print(srcs)
print(titles)