編輯我想用Python腳本來做到這一點: 更改鏈接的圖像鏈接,例:解析HTML文件不是與Python
<td>mylink.com</td>
到:
<td><a href="mylink,com"><img src="myimage.jpg"></a></td>
這個我試過用BeautifulSoup
LIB:
soup = BeautifulSoup("<td>mylink.html</td>")
soup.td.string.wrap(soup.new_tag("a"))
text = soup.a.string
soup.a.clear()
soup.find('a')['href'] = text
image = soup.new_tag('img')
soup.a.append(image)
soup.find('img')['src'] = "images/world_link.png"
它工作正常,但我想添加另一個屬性target
在<a href="" target=""
,我該怎麼做?
現在我想通過所有TD循環,我想這:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("C:\Users\Will\Desktop\htm.html"))
td = soup.find_all('td')
for s in td:
a = soup.new_tag("a", href=s.td.text, target='_blank')
img = soup.new_tag('img', src="images/world_link.png")
a.append(img)
s.td.string.replace_with(a)
但是這麼想的工作,我有這樣的錯誤:AttributeError的:「NoneType」對象有沒有屬性「文本」
什麼你試過了嗎?添加更多信息等。或者有人會提出問題並將被關閉。 – 2014-09-12 15:29:57
沒有您請求軟件的網站。所以,我必須舉報。 – 2014-09-12 15:33:37