Python Selenium Get All “href” attributes

前端 未结 2 1511
名媛妹妹
名媛妹妹 2020-12-22 10:46

How will I get all the \"href\" attributes for this \"h2\" titles on this page?

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-22 11:11

    Selenium might be overkill for what you need, good old BeautifulSoup will do the trick as well.

    import urllib.request, bs4
    body = urllib.request.urlopen(urllib.request.Request("http://www.allitebooks.com/page/1/?s=python", headers={"User-Agent": "Mozilla"})).read().decode("utf-8")
    soup = bs4.BeautifulSoup(body)
    for element in soup.find_all("h2", class_="entry-title"):
        for link in element.find_all("a"):
            print(link.get("href"))
    

提交回复
热议问题