beautifulsoup 相关问题

Beautiful Soup是一个用于解析HTML / XML的Python包。此软件包的最新版本是版本4,导入为bs4。

无法通过网页抓取在维基百科中找到特定表格?

我正在抓取以下维基百科页面:https://en.wikipedia.org/wiki/Eurovision_Song_Contest_2022。我已经能够从此页面中抓取另一个表格,但现在我想抓取“

回答 1 投票 0

无法通过网页抓取在维基百科中找到特定表格?

我正在抓取以下维基百科页面:https://en.wikipedia.org/wiki/Eurovision_Song_Contest_2022。我已经能够从此页面中抓取另一个表格,但现在我想抓取“

回答 1 投票 0

BeautifulSoup 抓取具有相同类名的标签

我是 BeautifulSoup 的新手。我正在使用 Python 和 bs 进行一些网络抓取,并且有两个段落具有相同的类名。 HTML如下: 2020 年 1 月 1 日星期三 00:01 ... 我是 BeautifulSoup 的新手。我正在使用 Python 和 bs 进行一些网络抓取,并且有两个段落具有相同的类名。 HTML如下: <p class='metadata'>Wed 1 Jan 2020 00:01 GMT</p><p class='metadata'>Category: <span>UK-News</span></p> 我正在尝试获取跨度标签内的类别名称(英国新闻)。当我抓取多篇文章时,我正在使用循环。这是我尝试过的一些事情: articles = soup.find_all('div', {'class' : 'article'}) for item in articles: #category = item.find('span') - prints out the same date #category = item.find('p', {'class' : 'metadata'}).text - prints every span tag in html #category = item.find('p', {'class' : 'metadata'}) - prints only the dates category = soup.select_one('span').get_text #prints out the same category name print(category) 这可能是一个小修复,但它真的让我很头疼,因为我觉得我已经尝试了一切。预先感谢。 您正在循环内的 soup 中搜索。将其更改为item.select_one: from bs4 import BeautifulSoup html_doc = """ <article> <p class='metadata'>Wed 1 Jan 2020 00:01 GMT</p> <p class='metadata'>Category: <span>UK-News</span></p> </article> <article> <p class='metadata'>Wed 2 Jan 2020 00:01 GMT</p> <p class='metadata'>Category: <span>World-News</span></p> </article>""" soup = BeautifulSoup(html_doc, "html.parser") articles = soup.find_all("article") for item in articles: category = item.select_one("p.metadata span").text # <-- use item.select print(category) 打印: UK-News World-News 试试这个: 对于文章中的项目: case1 = tag.select("div.ID").text case2 = tag.select("div.Id").next_sibling.text 打印(案例1,案例2)

回答 2 投票 0

如何在汤体内提取这个值

这是我的Python代码,它对API调用响应内容执行BeautifulSoup: 汤 = BeautifulSoup(resp.content, 'lxml') 如果我输出汤体,它看起来像这样: 这是我的 python 代码,它对 API 调用响应内容执行 BeautifulSoup: soup = BeautifulSoup(resp.content, 'lxml') 如果我输出汤体,它看起来像这样: <html> <body> .... <script src="/site_media/js/jquery/jquery.js" type="text/javascript"></script> <script nonce="" type="text/javascript"> var username_field = document.getElementById("id_username"); if(username_field.value){ document.getElementById("id_password").focus(); } else { username_field.focus(); } $(".toggle-password").click(function() { $(this).toggleClass("fa-eye fa-eye-slash"); var input = $($(this).attr("toggle")); if (input.attr("type") == "password") { input.attr("type", "text"); } else { input.attr("type", "password"); } }); var iam_login_link = document.getElementById("iam_login_link"); var iam_login_enabled = "False"; if (iam_login_enabled === 'True') { iam_login_link.style.display = '' } else { iam_login_link.style.display = 'none' } $('#iamLogin').on('click', function() { window.location.href = "/saml-idp/applebananapeach/iam_login/?SAMLRequest=BlaBlaBla"; }); </script> </body> </html> 我的问题是如何从汤中提取 window.location.href 链接? 认为在这种特定情况下不需要 beautiful soup,因为您必须从 JavaScript 中提取值,因此可以使用正则表达式: pattern = r'window\.location\.href\s*=\s*["\']([^"\']+)["\']' match = re.search(pattern, resp.content) if match: print(match.group(1)) else: print('not found')

回答 1 投票 0

从带有 Beautiful Soup 的标签获取文本,但其兄弟姐妹除外

我需要从(产品名称)获取除数量和价值之外的文本,但我没有找到任何代码。 我尝试在以下示例 H 中使用 soup.find 或 soup.select...

回答 1 投票 0

从带有漂亮汤的标签中获取文本,除了

1:EFURIX CREM 15 GR S VALEA <tbody> <tr> <td class="no-border" colspan="2"> <small> 1: EFURIX CREM 15 GR S VALEA <span class="pull-right"> </span> </small> <small> 1,00 x R$22,50 </small> <td class="no-border text-right"> <small> R$22,50 </small> </td> </td> </tr> <tr> <td class="no-border" colspan="2"> <small> 2: ASDRON XPE FR 100ML <span class="pull-right"> </span> </small> <small> 1,00 x R$50,32 </small> <td class="no-border text-right"> <small> R$50,32 </small> </td> </td> </tr> <tr> <td class="no-border" colspan="2"> <small> 3: DIAD 0,75MGC/ 2 COMP <span class="pull-right"> </span> </small> <small> 1,00 x R$5,00 </small> <td class="no-border text-right"> <small> R$5,00 </small> </td> </td> </tr> </tbody> 我需要从小标签(产品名称)中获取除数量和价值之外的文本,但我没有找到任何代码 我尝试使用 soup.find 或 soup.select 您可以使用带有伪类的 css selector 来始终获得第一个 <small>: soup.select('tr > td:first-of-type > small:first-of-type') 要根据您的示例获取所有标题,请使用 list comprhension: [title.get_text(strip=True).split(' ')[-1] for title in soup.select('tr > td:first-of-type > small:first-of-type')] 结果: ['EFURIX CREM 15 GR S VALEA', 'ASDRON XPE FR 100ML', 'DIAD 0,75MGC/ 2 COMP']

回答 1 投票 0

使用 BeautifulSoup 抓取世界指数表后出现空名称

我正在尝试从雅虎财经的世界指数表中抓取该指数的股票代码和全名:https://finance.yahoo.com/world-indices/ 这是我目前拥有的代码: 来自 BS4 ...

回答 1 投票 0

使用Python和Beautifulsoup用<h2>标记返回的文本,其中原始网页中存在标题2

我正在从网站上抓取一篇文章,并希望尽可能保留文本的原始格式。我的想法是让 beautifulsoup 返回所有文本,并使用一些 Python 代码来输入...

回答 1 投票 0

python 报废网站数据提供空值

我正在尝试废弃 MLB BOX 分数和逐场比赛信息。 导入请求 从 bs4 导入 BeautifulSoup url =“https://www.sportsnet.ca/baseball/mlb/games/2618275/” 回应 =

回答 1 投票 0

从包含java的网站中提取网站数据

我如何从该网站提取数据: https://maroof.sa/businesses/details/229217 我是刮擦初学者。

回答 1 投票 0

尝试使用 Beautifulsoup 抓取速卖通产品评论

我正在尝试从产品页面收集速卖通评论,例如 https://www.aliexpress.com/item/3256801798731854.html 我已经编写了代码来抓取此页面并收集评论。 我...

回答 4 投票 0

使用 beautifulsoup 更新 InstallShield .ism (MSI) 文件并保持其格式

我正在尝试使用当前的构建版本更新我的 .ism 文件。我可以毫无问题地查找和替换构建字符串。但是,我正在努力让我的 .ism 文件保持其格式...

回答 2 投票 0

使用 Selenium 和 Beautiful Soup 抓取 JavaScript 表

我正在尝试抓取这个网站:https://www.globusmedical.com/patent-education-musculoskeletal-system-conditions/resources/find-a-surgeon/ 该网站似乎使用了 JavaScript,因此...

回答 1 投票 0

如何从含有乱码元素的表格的每一行中抓取标题?

我正在尝试使用请求模块和 BeautifulSoup 库从此网页中抓取表格内容。 我已经设法获取包含一些乱码的表格的 HTML 元素

回答 1 投票 0

无法从存档中解析原始URL

我写信给你是因为 我想从存档中获取原始 URL。我尝试了不同的 Python 库,但无法解析存档链接示例 https://archive.ph/kEOqK 网址=“

回答 1 投票 0

无法使用BeautifulSoup访问Div内的img

我正在尝试使用Python中的BeautifulSoup访问图像的SRC。这是图像的嵌套方式: 我正在尝试使用 Python 中的 BeautifulSoup 访问图像的 SRC。这是图像的嵌套方式: <div class="artistAndEventInfo-7c13900b"> <a class="artistAndEventInfo-48455a81" href="https://www.bandsintown.com/a/11985-perkele?came_from=257&amp;utm_medium=web&amp;utm_source=artist_event_page&amp;utm_campaign=artist"> <img src="https://assets.bandsintown.com/images/fallbackImage.png" alt=""> </a> 我尝试了三种方法。 1:逻辑是我选择相关图像的父 div,然后选择其中的子 img: image = soup.select_one('[class^=artistAndEventInfo-7c13900b] img') print "band image", image 这将打印“none”。 (它应该输出SRC)。 2:使用更明确的第n个类型方法: image = soup.select_one('[class^=artistAndEventInfo-7c13900b] :nth-of-type(1) img') 但是输出仍然是“none”。 3:我也尝试过使用 Selenium: driver.find_element_by_xpath("//div[@class^=artistAndEventInfo-48455a81']") 这给了我错误: selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //div[@class^=artistAndEventInfo-7c13900b']/img because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[@class^=artistAndEventInfo-7c13900b']/img' is not a valid XPath expression. (Session info: chrome=74.0.3729.157) (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.11.6 x86_64) 为什么我的代码在所有这些情况下都不起作用? 您的 xpath 看起来有错误 //div[@class^=artistAndEventInfo-7c13900b']/img' 应该是 //div[@class='artistAndEventInfo-7c13900b']/img' 如果你想获取图像的src,那么你应该使用下面的代码和更正后的xpath。 print(driver.find_element_xpath("//div[@class='artistAndEventInfo-7c13900b']//img").get_attribute("src")) 如果您想使用选项 1 和 2,请确保您获得如下属性 src。 print image['src'] 使用 BeautifulSoup,你可以这样做: from bs4 import BeautifulSoup html = ''' <div class="artistAndEventInfo-7c13900b"> <a class="artistAndEventInfo-48455a81" href="https://www.bandsintown.com/a/11985-perkele?came_from=257&amp;utm_medium=web&amp;utm_source=artist_event_page&amp;utm_campaign=artist"> <img src="https://assets.bandsintown.com/images/fallbackImage.png" alt=""> </a> ''' soup = BeautifulSoup(html,'html5lib') img = soup.find('img') src = img['src'] print(src) 您的 div 标签类属性值可能是动态的。您可以尝试下面的方法,而不是使用完整的类属性值。 from bs4 import BeautifulSoup html='''<div class="artistAndEventInfo-7c13900b"> <a class="artistAndEventInfo-48455a81" href="https://www.bandsintown.com/a/11985-perkele?came_from=257&amp;utm_medium=web&amp;utm_source=artist_event_page&amp;utm_campaign=artist"> <img src="https://assets.bandsintown.com/images/fallbackImage.png" alt=""> </a>''' soup=BeautifulSoup(html,'lxml') image = soup.select_one('div[class^=artistAndEventInfo-] img') print(image['src'])

回答 4 投票 0

类型错误:某些关键字参数意外

我正在尝试为页面编写一个解析器。我正在使用 LxmlSoup 库。 所以协议是: html = requests.get('https://www.mcdonalds.com/ua/uk-ua/eat/fullmenu.html').text 汤 = LxmlSoup(html) 网址=汤。

回答 1 投票 0

为什么抓取的 HTML 与浏览器检查的元素不同?

我目前正在从事一个网络抓取项目,在从 https://Foundersfund.com/portfolio 抓取数据时遇到了问题。我设法检索到每个公司页面的所有链接

回答 1 投票 0

我使用requests和beautifulsoup来抓取一个网页,为什么我的程序中的html与inspect元素中的不一样?

我目前正在从事一个网络抓取项目,在从 https://Foundersfund.com/portfolio 抓取数据时遇到了问题。我设法检索到每个公司页面的所有链接

回答 1 投票 0

Python BeautifulSoup - 如何将嵌套元素转换为缩进文本

我想知道是否有人可以帮助我了解如何使用 BeautifulSoup 和 Python 获取网站抓取并将其转换为文本文件。这是来自留言板,人们在那里写自己的文字......

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.