Python BeautifulSoup 替换 img src
Posted
技术标签:
【中文标题】Python BeautifulSoup 替换 img src【英文标题】:Python BeautifulSoup replace img src 【发布时间】:2017-01-10 14:21:41 【问题描述】:我正在尝试从站点解析 html 内容,更改 href 和 img src。 a href 更改成功,但 img src 不成功。
它在变量中发生了变化,但在 HTML (post_content) 中没有:
<p><img src="https://lifehacker.ru/wp-content/uploads/2016/08/15120903sa_d2__1471520915-630x523.jpg" title="Title"/></p>
不是 _http://site.ru...
<p><img src="http://site.ru/wp-content/uploads/2016/08/15120903sa_d2__1471520915-630x523.jpg" title="Title"/></p>
我的代码
if "app-store" not in url:
r = requests.get("https://lifehacker.ru/2016/08/23/kak-vybrat-trimmer/")
soup = BeautifulSoup(r.content)
post_content = soup.find("div", "class", "post-content")
for tag in post_content():
for attribute in ["class", "id", "style", "height", "width", "sizes"]:
del tag[attribute]
for a in post_content.find_all('a'):
a['href'] = a['href'].replace("https://lifehacker.ru", "http://site.ru")
for img in post_content.find_all('img'):
img_urls = img['src']
if "https:" not in img_urls:
img_urls="http:".format(img_urls)
thumb_url = img_urls.split('/')
urllib.urlretrieve(img_urls, "/Users/kr/PycharmProjects/education_py//".format(folder_name, thumb_url[-1]))
file_url = "/Users/kr/PycharmProjects/education_py//".format(folder_name, thumb_url[-1])
data =
'name': ''.format(thumb_url[-1]),
'type': 'image/jpeg',
with open(file_url, 'rb') as img:
data['bits'] = xmlrpc_client.Binary(img.read())
response = client.call(media.UploadFile(data))
attachment_url = response['url']
img_urls = img_urls.replace(img_urls, attachment_url)
[s.extract() for s in post_content('script')]
post_content_insert = bleach.clean(post_content)
post_content_insert = post_content_insert.replace('<', '<')
post_content_insert = post_content_insert.replace('>', '>')
print post_content_insert
【问题讨论】:
【参考方案1】:看起来您从未将img_urls
分配回img['src']
。尝试在块的末尾这样做。
img_urls = img_urls.replace(img_urls, attachment_url)
img['src'] = img_urls
... 但是首先,您需要更改 with
语句,以便它使用除 img
之外的其他名称作为文件对象。现在你覆盖了 dom 元素,你不能再访问它。
with open(file_url, 'rb') as some_file:
data['bits'] = xmlrpc_client.Binary(some_file.read())
【讨论】:
已经尝试过但是 - img['src'] = img_urls TypeError: 'file' object does not support item assignment 哦,这是一个名称冲突问题。已编辑。 工作 - 完美,非常感谢,对于菜鸟问题感到抱歉。以上是关于Python BeautifulSoup 替换 img src的主要内容,如果未能解决你的问题,请参考以下文章
Python3.x:BeautifulSoup()解析网页内容出现乱码
Python爬虫连载12-爬虫正则表示式BeautifulSoup初步
如何仅使用BeautifulSoup和Python删除包含空格的HTML标记