1

这是网页的布局:

<h2>Featured Ads</h2>
<a href=""></a>

<h2>Ads</h2>
<a href=""></a>

常规广告中没有任何东西class可以用来区分它们。什么是只返回<a href>之后出现的 's的有效方法<h2>Ads</h2>

更新:

这是最终代码

h2 = soup.find("h2", text="Ads")
articles = h2.find_next_siblings("article")

for article in articles:
    for div in article.find_all('div', {'class': 'address'}):
        for link in div.find_all('a', href=True):
            print (link['href'])

更新 2:必须重构...

articles = soup.find("h2", text="Ads").find_next_siblings("article")
for article in articles:
    ad_url = article.find('a', href=True)['href']

wordpress中页面的自定义模板

是否可以为这不是首页的页面创建自定义 php 模板

例如:当您的主页有front-page.php 时,我想做一些类似的事情。

我不知道这是否可能,但如果是的话,谢谢您的回答

4

1 回答 1

2

找到h2元素并找到下一个a兄弟

h2 = soup.find("h2", text="Ads")
a = h2.find_next_sibling("a")
于 2015-10-26T01:07:20.967 回答