首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >排除Beautifulsoup中的图片链接

排除Beautifulsoup中的图片链接
EN

Stack Overflow用户
提问于 2020-01-14 07:11:25
回答 2查看 406关注 0票数 0

寻找一种方法来排除图像链接/不包含任何锚文本的链接。下面的代码完成了编译我想要的数据的工作,但它也从页面上的一些缩略图/图像链接中拾取了不需要的URL

代码语言:javascript
复制
for url in list_urls:
    browser.get(url)
    soup = BeautifulSoup(browser.page_source,"html.parser")
    for line in soup.find_all('a'):
        href = line.get('href')
        links_with_text.append([url, href])

抓取的页面上的图像都具有相同的格式(并且它们都在相同的div类“related-content”下):

代码语言:javascript
复制
<a href="https://XXXX/"    ><picture class="crp_thumb crp_featured" title="XXXX">
<source type="image/webp" srcset="https://XXXX.jpg.webp"/>
<img width="150" height="150" src="https://XXXX.jpg" alt="XXXX"/>
</picture>
EN

回答 2

Stack Overflow用户

发布于 2020-01-14 07:22:09

这里有几个你可以使用的例子:

  1. 选择不包含任何文本的<a>标记
  2. 选择不包含<img>标记的<a>标记
  3. 选择不包含任何文本的D9标记

代码语言:javascript
复制
txt = '''
<a href="https://XXXX/">
<picture class="crp_thumb crp_featured" title="XXXX">
<source type="image/webp" srcset="https://XXXX.jpg.webp"/>
<img width="150" height="150" src="https://XXXX.jpg" alt="XXXX"/>
</picture>
</a>

<a href="https://XXX">OK LINK</a>
'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(txt, 'html.parser')

# select <a> tags that don't contain any text
for a in soup.find_all(lambda t: t.name == 'a' and t.get_text(strip=True) != ''):
    print(a)

# select <a> tags that don't contain <img> tags
for a in soup.select('a:not(:has(img))'):
    print(a)

# select <a> tags that don't contain any text and <img> tags
for a in soup.find_all(lambda t: t.name == 'a' and t.get_text(strip=True) != '' and not t.find('img')):
    print(a)
票数 0
EN

Stack Overflow用户

发布于 2020-01-14 09:49:13

使用SimplifiedDoc的解决方案。

代码语言:javascript
复制
from simplified_scrapy.simplified_doc import SimplifiedDoc
html='''<a href="https://XXXX/"    ><picture class="crp_thumb crp_featured" title="XXXX">
<source type="image/webp" srcset="https://XXXX.jpg.webp"/>
<img width="150" height="150" src="https://XXXX.jpg" alt="XXXX"/>
</picture></a>'''
doc = SimplifiedDoc(html)
lstA = doc.getElementsByTag('a')
lstImg = doc.getElementsByTag('img')
lstSource = doc.getElementsByTag('source')
print ([a.href for a in lstA])
print ([img.src for img in lstImg])
print ([source.srcset for source in lstSource])
lstA = doc.getElementsByTag('a').notContains('<picture')
print ([a.href for a in lstA])

结果:

代码语言:javascript
复制
['https://XXXX/']
['https://XXXX.jpg']
['https://XXXX.jpg.webp']
[]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59725518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档