Can't retreive href with BeautifulSoup
我正在尝试用漂亮的汤在for循环中检索hrefs。我已经用一些
1 | events = soup.find_all("a", attrs={"class":"event-link-wrap"}) |
然后我运行这样的for循环:
1 2 3 4 5 | for event in events: href = event.find("href") category = event.find("p",{"class":"category"}) title = event.find("h3") arena = event.find("span", {"class":"venue"}) |
号
当我打印Href时,我得到
1 2 3 4 5 6 7 8 9 10 11 | <img alt="pic_125x125.jpg" src="https://www.test.com/pic.jpg"/> <p class="category">CATEGORY HERE </p> EVENT TITLE HERE <p class="date"><span class="m-date__rangeFirst"><span class="m-date__day"> 6 </span></span><span class="m-date__separator"> - </span><span class="m-date__rangeLast"><span class="m-date__day"> 7 </span><span class="m-date__month">april</span></span> <span class="venue"> ARENA HERE</span> </p> <span class="icon"></span> <span class="icon-hover"></span> |
我想要的href在第一个标签中。我可以检索我想要的一切,除了href。我怎样才能得到这个href?就像我提到的,现在它所返回的是
您可以通过
1 | events = [i['href'] for i in soup.find_all("a", attrs={"class":"event-link-wrap"})] |
由于您正在循环遍历包含您要查找的
只需使用:
1 2 3 | for event in events: href = event["href"] ... |
尝试:
1 2 3 | events = soup.find_all("a", class_="event-link-wrap") for event in events: href = event.get("href") |
号