python - 如何本地化为我提供 url 的正确 xpath?
问题描述
我正在尝试通过 python 抓取一些 URL。特别是,我正在研究与香港选举平台的链接。我使用了chrome提供的inspect功能,在python上也使用了lxml的etree功能。我已经找到了“文本”的 xpath。
我使用的代码是
def extract_info_urls(self, response):
raw_tree = etree.HTML(response)
platform_urls = raw_tree.xpath('//*[@id="table-district-member"]/tbody/tr/td[6]/div/a/@href|//*[@id="table-district-member"]/tbody/tr/td[4]/div/a/@href')
return platform_urls
结果如下所示:
../../pdf/intro_to_can/A01_1_ENG.html
综上所述,我的谦虚问题是如何获得完整的网址 - https://www.elections.gov.hk/dc2019/pdf/intro_to_can/A01_1_ENG.html - 而不仅仅是以“.. /../pdf”在结果中。
这是我在这里的第一个问题,可能很愚蠢。但我感谢你的所有帮助。在这里期待与大家一起学习!
非常感谢你。
解决方案
要获取完整的 URL,您可以替换
return platform_urls
和
return ["https://www.elections.gov.hk/dc2019/" + platform_url.lstrip("../..") for platform_url in platform_urls]
推荐阅读
- spring-batch - Spring Batch 中远程块处理中的事务管理(在工作人员端)
- flutter - 什么是正确的提供者
为我的 DrawerNavigation 小部件? - java - 从实体列表中查找缺失的实体
- pandas - 熊猫时期 to_timestamp 给我 TypeError
- ios - 根据表格视图swift中选择的Indexpath过滤字典数组
- machine-learning - 使用机器学习检测钓鱼网站
- ajax - 如何使用 Ajax 从 Controller 获取 DataTable 的数据到 View
- terraform - 如何让 Terraform azuread_groups 接受 UPN 而不是 object_id?
- regex - 正则表达式忽略第二对括号(如果有)
- python - 使用 BeautifulSoup 从 Web 服务器中抓取变量