gpt4 book ai didi

Python - Beautifulsoup 到带有图片的 PDF(相对路径)

转载 作者:行者123 更新时间:2023-12-01 04:26:27 27 4
gpt4 key购买 nike

我使用 mechanize 浏览网站。之后,我使用 beautifulsoup 来操作网页的内容(转换为 unicode,删除一些行)。现在我想从使用 Beautifulsoup 获得的 html 源创建 PDF 文件。我使用 pdfkit,它对于文本效果很好。但现在我想用 html 代码中的图片创建 pdf。 url 通过使用相对路径“../../”等指定(也适用于图片)。

如何更改所有 url 以考虑绝对路径以及如何获取 pdf 文件中的图片?路径的改变足以获取图片吗?

解决方案:(基于 dudu1791 提案)

#changement liens vers images
def ChangeLinkIMG(soup,baseurl):
#parcours des images
for imgLK in soup.findAll('img'):
#chemin relatif image
try:
relaIMG=imgLK['src']
#creation lien absolu
absoIMG=urljoin(baseurl,relaIMG)
imgLK['src']=absoIMG
print absoIMG
except:
pass
return soup

最佳答案

这可能是答案的一半,但下面的代码可以帮助您将 url 转变为考虑绝对路径。我就是这样做的。

def parse_all_links(self, soup):            
for link in soup.find_all('a'):
if(link.get('href')):
href = link.get('href')
if href.startswith('http') or href.startswith('https'):
print(href)
elif href =='#':
#print('No link present')
pass
elif href =='/':
pass
else:
href = baseurl + href
print(href)

关于Python - Beautifulsoup 到带有图片的 PDF(相对路径),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33068747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com