Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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 3.x 如何用新属性替换属性及其内容?_Python 3.x_Beautifulsoup_Css Selectors - Fatal编程技术网

Python 3.x 如何用新属性替换属性及其内容?

Python 3.x 如何用新属性替换属性及其内容?,python-3.x,beautifulsoup,css-selectors,Python 3.x,Beautifulsoup,Css Selectors,我有一个用classlink right verbtable标记的html,我想在其中替换属性href及其值,即 href=”https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer“ 用一个新的 old_onclick=“expandfullverbtable();”onclick=“expandfullverbtable();”data text=“+” 一种方法是将此html作为字符串读取并使

我有一个用class
link right verbtable
标记的html,我想在其中替换属性
href
及其值,即

href=”https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer“

用一个新的

old_onclick=“expandfullverbtable();”onclick=“expandfullverbtable();”data text=“+”

一种方法是将此html作为字符串读取并使用函数
replace
。由于此内容来自html,我想请求使用
BeautifulSoup
的方法

from bs4 import BeautifulSoup

html = '<a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table</a>'
soup = BeautifulSoup(html, 'html.parser')
从bs4导入美化组
html=“”
soup=BeautifulSoup(html,'html.parser')
非常感谢你的帮助

试试这个:

from bs4 import BeautifulSoup
remove = ['href']
new =['old_onclick', 'onclick', 'data-text']
values = ['expandfullverbtable();', 'expandfullverbtable();', '"+"']
html = '''
    <a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table</a>
    <a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table</a>
    <a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table</a>
'''
soup = BeautifulSoup(html, 'html.parser')
for a in soup.select('a.link-right.verbtable'):
    for n, v in zip(new, values):
        a[n] = v
    a.attrs = {key:value for key,value in a.attrs.items()
               if key not in remove}
print(soup)
从bs4导入美化组
删除=['href']
新=['old_onclick'、'onclick'、'数据文本']
值=['expandfullverbtable();','expandfullverbtable();','“+”]
html=“”
'''
soup=BeautifulSoup(html,'html.parser')
对于汤中的。选择('a.link-right.verbtable'):
对于zip中的n,v(新值):
a[n]=v
a、 attrs={key:key的值,a.attrs.items()中的值
如果密钥不在remove}
印花(汤)
印刷品:

<a class="link-right verbtable" data-text='"+"' old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
<a class="link-right verbtable" data-text='"+"' old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
<a class="link-right verbtable" data-text='"+"' old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
完整动词表
全动词表
全动词表

您可以使用此脚本替换所有选定的
href=
''' soup=BeautifulSoup(html,'html.parser') 对于汤中的。选择('a.link-right.verbtable'): dela['href'] a['old_onclick']='expandfullverbtable();' a['onclick']='expandfullverbtable();' a['data-text']='+' 印花(汤) 印刷品:

<a class="link-right verbtable" data-text="+" old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
<a class="link-right verbtable" data-text="+" old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
<a class="link-right verbtable" data-text="+" old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
完整动词表
全动词表
全动词表

我希望我已经理解了你的问题。。。您有一个答案@lad。由类
链接右侧的verbtable
标记的内容会多次出现,而您的代码使用
。请选择一个
。您能修改它来替换所有这样的实例吗?这也解决了多次替换的情况。非常感谢你!
<a class="link-right verbtable" data-text="+" old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
<a class="link-right verbtable" data-text="+" old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>
<a class="link-right verbtable" data-text="+" old_onclick="expandfullverbtable();" onclick="expandfullverbtable();">Full verb table</a>