python - 用python3.7修剪列表中的链接
问题描述
我在 python3.7 中有一个小脚本(请参见此处的相关问题),它从网站(http://digesto.asamblea.gob.ni/consultas/coleccion/)中抓取链接并将它们保存在列表中。不幸的是,它们只是部分的,我必须修剪它们以将它们用作链接。
这是脚本的相关部分:
list_of_links = [] # will hold the scraped links
tld = 'http://digesto.asamblea.gob.ni'
current_url = driver.current_url # for any links not starting with /
table_id = driver.find_element(By.ID, 'tableDocCollection')
rows = table_id.find_elements_by_css_selector("tbody tr") # get all table rows
for row in rows:
row.find_element_by_css_selector('button').click()
link = row.find_element_by_css_selector('li a[onclick*=pdf]').get_attribute("onclick") # href
print(list_of_links)# trim
if link.startswith('/'):
list_of_links.append(tld + link)
else:
list_of_links.append(current_url + link)
row.find_element_by_css_selector('button').click()
print(list_of_links)
我怎样才能操纵这个列表(仅作为示例,这里只有三个条目)
["http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;", "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=Z%2FgLeZxynkg%3D');return false;", "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=9rka%2BmYwvYM%3D');return false;"]
好像
["http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D", "http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=Z%2FgLeZxynkg%3D", "http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=9rka%2BmYwvYM%3D"]
分解:在第一个链接的例子中,我从网站上得到这个链接基本上是
http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;
并且需要将其修剪为
http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D
.
如何在整个列表中的 python 中实现这一点?
解决方案
这应该可以解决问题:
s = "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;"
s = s.replace("/consultas/coleccion/window.open('", "").replace("');return false;", "")
推荐阅读
- python - 在 PIL.ImageDraw.draw 中使用字体名称而不是文件路径
- visual-studio-code - 如何在 vscode 中显示非 ASCII 字符
- oracle - Oracle - 如何从查询创建 csv 字符串
- amazon-web-services - AWS API Gateway 返回 TARGET_MODEL_HEADER_MISSING,即使目标模型在标头中传递
- ios - iOS15 B4 NSPersistentCloudKitContainer“帐户暂时不可用”错误
- python - Python解压缩并将“_”添加到新提取的文件中
- asp.net-core - 有人能解释一下 ASP.NET 中的生产和开发是如何工作的吗
- python - 其他谷歌云服务账户的云作曲家认证
- c# - 如何检测 Windows 是否支持运行 x64 进程?
- c# - 如何在asp.net c#中将子页面的内容加载到div中