【问题标题】:getting a list out of a website /selenium /python从网站/selenium /python 中获取列表
【发布时间】:2021-03-26 19:34:47
【问题描述】:

我正在尝试从类似的元素中获取列表

<a href="/Max/Project" itemprop="name codeRepository">Project</a>

xpath 是

/html/body/div[4]/main/div[2]/div/div[2]/div[2]/div/div[2]/ul/li[1]/div[1]/div[1]/h3/a

我已经试过了

ids = driver.find_elements_by_xpath("a[@id='user-repositories-list']")

但它没有显示包含此 git 用户的存储库的列表。

BR,谢谢

【问题讨论】:

  • 顺便说一句。它应该是一个列出所有 git repos 的列表。

标签: python selenium web-scraping xpath


【解决方案1】:

您可以像这样检索 git 用户的存储库链接:

repos = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@id='user-repositories-list']/ul/li/div/div/h3/a")))

repo_names = []
for repo in repos:
    href = repo.get_attribute("href")
    href = href.rsplit('/', 1)[1]
    repo_names.append(href)

print(repo_names)

然后,您只需要进行一些文本解析即可从链接中提取 repo 的名称。

注意 html 的嵌套结构。在 id='user-repositories-list' 下,可以找到 all 存储库的块。 h3 代表三阶标头,a 标签表示超链接。 WebDriverWait 确保等到所有元素都可见并且可以找到。

【讨论】:

    猜你喜欢
    • 2021-04-11
    • 2022-08-18
    • 2017-04-01
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多