- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很难尝试使用 beautifulsoup 从页面中提取超链接。我尝试了许多不同的标签和类,但如果没有一大堆我不想要的其他 html,似乎无法获得它。有人能告诉我哪里错了吗?代码如下:
from bs4 import BeautifulSoup
import requests
page_link = url
page_response = requests.get(page_link, timeout=5)
soup = BeautifulSoup(page_response.content, "html.parser")
pagecode = soup.find(class_='infinite-scroll-container')
title = pagecode.findAll('i')
artist = pagecode.find_all('h1', "exhibition-title")
links = pagecode.find_all('article', "teaser infinite-scroll-item")
printcount=0
while printcount < len(title):
titlestring = title[printcount].text
artiststring = artist[printcount].text
artiststring = artiststring.replace(titlestring, '')
artiststring = artiststring.strip()
titlestring = titlestring.strip()
print(artiststring)
print(titlestring)
print("----------------------------------------")
printcount = printcount+1
最佳答案
您可以直接定位该页面中的所有链接,然后对其进行过滤以获取文章中的链接。请注意,此页面仅在滚动时完全加载,您可能必须使用 selenium获取所有链接。现在我将回答如何过滤链接。
from bs4 import BeautifulSoup
import requests
import re
page_link = 'https://hopkinsonmossman.com/exhibitions/past/'
page_response = requests.get(page_link, timeout=5)
soup = BeautifulSoup(page_response.content, "html.parser")
links= soup.find_all('a')
for link in links:
if link.parent.name=='article':#only article links
print(re.sub(r"\s\s+", " ", link.text).strip())#replace multiple spaces with one
print(link['href'])
print()
输出
Nicola Farquhar A Holotype Heart 22 Nov – 21 Dec 2018 Wellington
https://hopkinsonmossman.com/exhibitions/nicola-farquhar-5/
Bill Culbert Desk Lamp, Crash 19 Oct – 17 Nov 2018 Wellington
https://hopkinsonmossman.com/exhibitions/bill-culbert-2/
Nick Austin, Ammon Ngakuru Many Happy Returns 18 Oct – 15 Nov 2018 Auckland
https://hopkinsonmossman.com/exhibitions/nick-austin-ammon-ngakuru/
Dane Mitchell Tuning 13 Sep – 13 Oct 2018 Wellington
https://hopkinsonmossman.com/exhibitions/dane-mitchell-4/
Shannon Te Ao my life as a tunnel 08 Sep – 13 Oct 2018 Auckland
https://hopkinsonmossman.com/exhibitions/shannon-te-ao/
Tilt Anoushka Akel, Ruth Buchanan, Meg Porteous 16 Aug – 08 Sep 2018 Wellington
https://hopkinsonmossman.com/exhibitions/anoushka-akel-ruth-buchanan-meg-porteous/
Shadow Work Fiona Connor, Oliver Perkins 02 Aug – 01 Sep 2018 Auckland
https://hopkinsonmossman.com/exhibitions/group-show/
Emma McIntyre Rose on red 13 Jul – 11 Aug 2018 Wellington
https://hopkinsonmossman.com/exhibitions/emma-mcintyre-2/
Tahi Moore Incomprehensible public fictions: Writer fights politician in car park 04 Jul – 28 Jul 2018 Auckland
https://hopkinsonmossman.com/exhibitions/tahi-moore-2/
Oliver Perkins Bleeding Edge 01 Jun – 07 Jul 2018 Wellington
https://hopkinsonmossman.com/exhibitions/oliver-perkins-2/
Spinning Phillip Lai, Peter Robinson 19 May – 23 Jun 2018 Auckland
https://hopkinsonmossman.com/exhibitions/1437/
Milli Jannides Cavewoman 19 Apr – 26 May 2018 Wellington
https://hopkinsonmossman.com/exhibitions/milli-jannides/
Oscar Enberg Taste & Power, a prologue 06 Apr – 12 May 2018 Auckland
https://hopkinsonmossman.com/exhibitions/oscar-enberg/
Fiona Connor Closed Down Clubs & Monochromes 09 Mar – 14 Apr 2018 Wellington
https://hopkinsonmossman.com/exhibitions/closed-down-clubs-and-monochromes/
Bill Culbert Colour Theory, Window Mobile 02 Mar – 29 Mar 2018 Auckland
https://hopkinsonmossman.com/exhibitions/colour-theory-window-mobile/
Role Models Curated by Rob McKenzie
Robert Bittenbender, Ellen Cantor, Jennifer McCamley, Josef Strau 26 Jan – 24 Feb 2018 Auckland
https://hopkinsonmossman.com/exhibitions/role-models/
Emma McIntyre Pink Square Sways 24 Nov – 23 Dec 2017 Auckland
https://hopkinsonmossman.com/exhibitions/emma-mcintyre/
我最初的想法是使用“ajax-link”类,但事实证明“HOPKINSON MOSSMAN”链接也有该类。您还可以使用该方法并过滤掉 find_all
中的第一个链接,这将给您相同的结果。
from bs4 import BeautifulSoup
import requests
import re
page_link = 'https://hopkinsonmossman.com/exhibitions/past/'
page_response = requests.get(page_link, timeout=5)
soup = BeautifulSoup(page_response.content, "html.parser")
links= soup.find_all('a',class_='ajax-link')
for link in links[1:]:
print(re.sub(r"\s\s+", " ", link.text).strip())#replace multiple spaces with one
print(link['href'])
print()
关于python - 在 Python 中查找 beautifulsoup 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54281358/
HTML 在 div 中包含字符串: 'div class="slide"' 'img src="xttps://site.com/files/r_1000,kljg894/43k5j/35h43jk
我用这个方法 allcity = dom.body.findAll(attrs={'id' : re.compile("\d{1,2}")}) 返回这样的列表: [掳虏驴碌路驴碌脴虏煤脨脜脧垄脥酶 隆
我正在使用 Jupyter 笔记本、Python 3.5 和虚拟环境。 在我的虚拟环境中,我做了: (venv) > pip install BeautifulSoup4 这似乎运行良好 b/c 终端
我打算用 GUI 制作一个字典程序,但我在第一个障碍上就失败了。我刚刚安装了一个模块( PyDictionary ),但是当我运行以下代码时出现错误。 from PyDictionary import
我正在将一些解析器从 BeautifulSoup3 迁移到 BeautifulSoup4,我认为考虑到 lxml 非常快并且它是我在 BS4 中使用的解析器,分析它会变得多快是个好主意,这里是分析结果
这个问题在这里已经有了答案: Getting Python error "from: can't read /var/mail/Bio" (6 个答案) 关闭 11 个月前。 From Beauti
目前我无法输入这个,因为根据 top,我的处理器是 100%,我的内存是 85.7%,都被 python 占用了。 为什么?因为我让它通过一个 250 兆的文件来删除标记。 250兆,就是这样!我一直
我写了下面的代码: from bs4 import BeautifulSoup import sys # where is the sys module in the source code fold
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
通常,当我尝试使用BeautifulSoup解析网页时,BeautifulSoup函数会得到NONE结果,否则就会引发AttributeError。。以下是一些独立的(即,由于数据是硬编码的,不需要访
我正在为一个项目使用 BeautifulSoup。这是我的 HTML 结构 John Sam Bailey Jack
这段代码正确地从我的博客中提取了马拉地语文本。我很欣赏使用漂亮的汤和正则表达式是多么容易。 from bs4 import BeautifulSoup import requests, re url
我想获取 HTML 中隐藏输入字段的值。 我想用 Python 编写一个正则表达式,它将返回 fooId 的值,前提是我知道 HTML 中的行遵循以下格式 有人可以提供一个 Python 示例来解
我正在尝试获取特定月份的所有链接、标题和日期,例如网站上的三月,我正在使用 BeautifulSoup 这样做: from bs4 import BeautifulSoup import reques
我正试图通过此链接收集有关 2020 年世界上收入最高的运动员收入的信息 https://www.forbes.com/profile/roger-federer/?list=athletes这是第一
我正在尝试从带有美丽汤的网页中捕获所有相关链接。我需要的所有链接都有 class="btn btn-gray"还有文字 More Info<> 仅提取这些链接的最佳方法是什么? 最佳答案 这个怎么样?
我正在尝试抓取一个具有下拉菜单的站点,用户可以在其中选择要显示的数据的年份。但是,我似乎被困在我的实现中。 这是网站网址:https://www.pgatour.com/tournaments/mas
我正在使用 BeautifulSoup 和 mechanise 从网页中查找一些内容。问题是有时找不到我正在寻找的字符串。我不知道有什么问题 对于许多网页,它可以正常工作数月,但突然停止工作。然后我必
( 更新代码 就在下面) 我有一个类:UrlData , 生成一个 url 列表: for url in urls: rawMechSiteInfo = mech.open(url) #me
这是我到目前为止所做的事情: import requests from bs4 import BeautifulSoup URL = "https://www.google.com/search?q=
我是一名优秀的程序员,十分优秀!