beautifulsoup 相关问题

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

BeautifulSoup:迭代元素列表并按类提取文本

尝试使用Python并提高一些报废技巧。 以下代码应从科学文章中提取作者和所属机构信息: def 主要(网址): r = requests.get(url...

回答 1 投票 0

FeatureNotFound:找不到具有您请求的功能的树构建器:html5lib

在我学习的课程中,建议执行以下代码: (这个想法是从维基百科页面获取第七个表) 数据 = requests.get("https://en.wikipedia.org/wiki/

回答 1 投票 0

使用 BS4 和 Requests-HTML 进行抓取仅适用于第一页,然后(“NoneType”对象没有属性“find”)

我是使用 Python 和 BeautifulSoup 进行网络抓取的新手,我正在尝试使用 Requests-HTML 从公共网站提取汽车数据(型号、价格等)。我可以成功地从...输出我需要的数据

回答 1 投票 0

容器不会在网页抓取中使用 BeautifulSoup 返回其内部的价格

我在尝试抓取页面时遇到问题(https://www.jumbo.com.ar/hogar-y-textil/ropa-de-cama/sabanas)。我正在尝试打印出应该包含所有产品信息的元素...

回答 1 投票 0

我正在尝试使用 bs4 打开 html 文件,但出现此错误

MarkupResemblesLocatorWarning:输入看起来更像是文件名而不是标记。您可能想要打开此文件并将文件句柄传递到 Beautiful Soup 中。 汤 = BeautifulSoup(html_doc,"lxml...

回答 1 投票 0

soup 查找之前出现在 DOM 中的特定“th”元素的链接

我尝试在 HTML 中查找特定链接。我有这个代码 website_link = infobox.find('a', class_='external', href=True) 这会找到多个链接。但我只想要 td 元素中的链接,...

回答 1 投票 0

从investing.com 抓取历史数据

我想抓取财务报告的历史数据。然而,我想出了如何抓取investing.com显示的表格,但如果我想要旧的数据,我不能,因为investing有一个“显示更多...

回答 1 投票 0

Python 爬行 JSON - 取回所有项目

我目前面临的问题是无法从特定网站上抓取我想要的信息。 详细来说,我想拿回观光的所有项目和价格......

回答 2 投票 0

如果使用 beautifulsoup main 存在,如何在主标签中查找 img?

我想知道如果主标签存在,如何查找主标签中的所有图像?除了只是找到标签外的图像(我想下一个订单来抓取主标签中的图像(如果存在),如果不存在......

回答 1 投票 0

无法从网站获取 Python 和 Postman 中的 GET 请求

一直在尝试抓取这个网站:“https://www.loopnet.com/search/commercial-real-estate/usa/for-lease/”,但它一直在尝试永远执行获取请求,即使在邮递员,我有点...

回答 1 投票 0

使用美丽汤进行网页抓取

我已经为一个网站完成了此操作,然后第二个网站上的html代码有很大不同。 我试图在网上找到有关如何以不同方式编写代码的信息,但运气不佳。 我是

回答 1 投票 0

python beautifulsoup 或 javascript 查找电话号码问题

我正在抓取一个具有 https://www.europages.co.uk/ARTISAN-4-SEASON-PROJECTS/00000005443350-781559001.html 等链接的网站。 我可以抓取公司名称、地址等,但无法获取电话号码......

回答 1 投票 0

我在 <a tag in python

我正在尝试通过足球网站页面获取页面的链接。我已经把拿出来了 我正在尝试通过足球网站页面获取页面链接。我已经拉出了 <a tags,但为了得到 'href',,它出来时是空的。 自从经历了叠加以来,我尝试了不同的方法。 with open("premier_league/page_2.html", encoding= 'utf-8') as f: page = f.read() parsed_page = BeautifulSoup(html, "html.parser") links = parsed_page.find_all("a") 上面的代码生成了所有 "href" 不起作用。 我最初尝试过: links = [ l.get('href') for l in links] links = [l for l in links if l and "/all_comps/shooting" in l] 我收到错误: ResultSet 对象没有属性 'get'。 .get 函数是 str 属性。 我将获取函数更改为: links = [ l['href] for l in links] .throws in an error that the [ ] can't be used for a str The next was to try: for links in parsed_page("a", href = True): try: print (links['href']) except KeyError: pass this worked. The results: /en/squads/b8fd03ef/2022-2023/c602/Manchester-City-Stats-FA-Community-Shield /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/schedule/Manchester-City-Scores-and-Fixtures-All-Competitions /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/shooting/Manchester-City-Match-Logs-All-Competitions /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/keeper/Manchester-City-Match-Logs-All-Competitions /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/passing/Manchester-City-Match-Logs-All-Competitions /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/passing_types/Manchester-City-Match-Logs-All-Competitions /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/gca/Manchester-City-Match-Logs-All-Competitions /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/defense/Manchester-City-Match-Logs-All-Competitions /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/possession/Manchester-City-Match-Logs-All- 我需要更改上面的代码以列表理解,但由于某些原因它没有响应: href_list = [link['href'] for link in parsed_page("a", href=True) if 'href' in link] href_list [ ] - empty list 我不明白。为什么不起作用?? 我还尝试了另一种格式。因为我想要的链接看起来像 /en/squads/b8fd03ef/2022-2023/matchlogs/all_comps/shooting/Manchester-City-Match-Logs-All-Competitions 所以: or link in links: if "matchlogs/all_comps/shooting/" in link: link else: pass 还是空的。 到目前为止我做错了什么。我已经使用 .get 和 links['href'] 方法尝试获取 href 属性,但没有成功。 您非常接近,但您只需要缩小搜索范围。现在,您正在抓取每个链接的全部内容,最终得到几层 XML(我相信,自从我接触 BeautifulSoup 以来已经有 10 年了。 您只需进一步指定,您正在专门查找所有 href 元素内部的 a 属性。我发现另一个与你的问题非常相似的问题已经得到了更彻底的回答,但本质上是它的要点: 您可以通过以下方式使用 find_all 来查找每个具有 href 属性的 a 元素,并打印每个元素: # Python2 from BeautifulSoup import BeautifulSoup html = '''<a href="some_url">next</a> <span class="class"><a href="another_url">later</a></span>''' soup = BeautifulSoup(html) for a in soup.find_all('a', href=True): print "Found the URL:", a['href'] # The output would be: # Found the URL: some_url # Found the URL: another_url # Python3 from bs4 import BeautifulSoup html = '''<a href="https://some_url.com">next</a> <span class="class"> <a href="https://some_other_url.com">another_url</a></span>''' soup = BeautifulSoup(html) for a in soup.find_all('a', href=True): print("Found the URL:", a['href']) # The output would be: # Found the URL: https://some_url.com # Found the URL: https://some_other_url.com 我不确定你使用的是哪个版本的Python或BeautifulSoup,但你应该注意这一点,因为他们在新版本中改变了一些小东西,如果你没有意识到的话,这些小东西可以帮助你。 BeautifulSoup 获取 href [重复]

回答 1 投票 0

在 Vercel 上部署 Flask Web 抓取应用程序时出错

👋 我一直在使用 Flask 开发我的第一个网页抓取应用程序,它在我的笔记本电脑上运行顺利。但是,我在尝试在 Vercel 上部署它时遇到了问题。错误消息显示:“功能...

回答 1 投票 0

如何使用美丽的汤来选择特定的餐桌?

我已经搜索了一段时间,但找不到答案。 我正在尝试通过以下链接抓取维基百科上可用的“各州结果”表: https://en.wik...

回答 1 投票 0

使用美丽的汤获得特定的桌子

我已经搜索了一段时间,但找不到答案。 我正在尝试通过以下链接抓取维基百科上可用的“各州结果”表: https://en.wik...

回答 1 投票 0

如何使用python绕过cloudflare

我无法抓取此网站 https://www.mentalhealthforum.net/,我收到 403 状态代码,即使我已经尝试了互联网上的所有可用解决方案。 Cloudflare 具有 h-captcha 保护...

回答 4 投票 0

你能用BeautifulSoup找到精确的html标签吗?

我正在使用 BeautifulSoup 向某些 HTML 代码文件添加任意属性。我们称之为...数据定制。 我们假设这个片段是 BeautifulSoup 将要加载的 html。 我正在使用 BeautifulSoup 向某些 HTML 代码文件添加任意属性。我们称之为...数据自定义。 我们假设这个片段是 BeautifulSoup 将要加载的 html。 <div class="col-md-6"> <label asp-for="Driller.FirstName"></label> <input asp-for="Driller.FirstName" class="form-control" /> </div> 现在是代码: tagId = 'theonetochange' outerHtml = '<input asp-for="Driller.FirstName" class="form-control" />' soup = BeautifulSoup(html, "html.parser") # Find by ID tag = soup.find(id = tagId) # If that didn't work, find the exact HTML code if tag is None: tag = soup.find(string = outerHtml) # This is what doesn't work if tag is None: print("Target not found") exit() # Add the custom attribute tag["data-custom"] = "mycustomdata" # Print out the results print(str(soup)) 我想最终得到这个: <div class="col-md-6"> <label asp-for="Driller.FirstName"></label> <input data-custom="customdata" asp-for="Driller.FirstName" class="form-control" /> </div> 由于 html 不包含该 ID,因此它应该使用我提供的确切 HTML 代码进行搜索,但看起来 soup.find(string = "...") 可能仅适用于标签的内容,而不适用于标签本身?有没有办法以这种方式选择整个标签? 从您的问题和评论中的讨论来看,您似乎遇到了所谓的 XY 问题。 这在编程中非常常见,通常是由称为“工具定律”(通常称为“金锤”)的常见反模式产生的。一旦您知道如何使用特定工具,您就可以用它来解决每个问题,因为每个问题看起来都可以用该工具解决。这可能是真的,但锤子很少是拧入螺丝的最佳工具。 这里你问的是如何使用 beautifulsoup 来查找特定的 HTML 片段,以便你可以用类似的片段替换它。具体来说,您想要找到这样的片段: <input asp-for="Driller.FirstName" class="form-control" /> 并将它们替换为: <input data-custom="customdata" asp-for="Driller.FirstName" class="form-control" /> 一种非常简单的方法是明目张胆的字符串替换,但您可能已经(正确地)发现这对 HTML 格式的微小变化非常敏感。 您也许可以使用 bs4 来实现此目的,并且用 HTML 文本替换 HTML 文本的方法似乎可行,但是 bs4 的默认解析器 lxml 实际上已经拥有您以更简单的方式执行此操作所需的一切以及更强劲的时尚:import bs4 from lxml import etree # this is just a small example document, you can use etree to load a file as well document = ''' <html> <body> <div class="col-md-6"> <label asp-for="Driller.FirstName"></label> <input asp-for="Driller.FirstName" class="form-control" /> </div> <div class="col-md-6"> <label asp-for="Driller.FirstName"></label> <input class="form-control" asp-for="Driller.FirstName" /> </div> <div class="col-md-6"> <label asp-for="Driller.FirstName"></label> <input class="form-control" /> <input asp-for="Driller.FirstName" /> <input class="form-control" asp-for="Driller.OtherName" /> <input class="not-form-control" asp-for="Driller.FirstName" /> </div> </body> </html> ''' # loading the document, for a file you'd use `etree.parse` doc = etree.fromstring(document, parser=etree.HTMLParser()) # loop over exactly the elements you want to change, using XPath to select them for element in doc.xpath('//input[@class="form-control" and @asp-for="Driller.FirstName"]'): element.attrib['data-custom'] = 'mycustomdata' print(etree.tostring(doc).decode()) 输出: <html> <body> <div class="col-md-6"> <label asp-for="Driller.FirstName"/> <input asp-for="Driller.FirstName" class="form-control" data-custom="mycustomdata"/> </div> <div class="col-md-6"> <label asp-for="Driller.FirstName"/> <input class="form-control" asp-for="Driller.FirstName" data-custom="mycustomdata"/> </div> <div class="col-md-6"> <label asp-for="Driller.FirstName"/> <input class="form-control"/> <input asp-for="Driller.FirstName"/> <input class="form-control" asp-for="Driller.OtherName"/> <input class="not-form-control" asp-for="Driller.FirstName"/> </div> </body> </html> 请注意,您询问是否将 data-custom 属性放在第一位。这应该不重要,因为 XML 和 HTML 中的属性顺序应该是没有意义的,但如果您正在处理某些需要将其放在第一位的系统,您可以通过替换 attrib 赋值来做到这一点: # keep the old attributes in order, leave out data-custom if it was there old_attrib = {k: v for k, v in element.attrib.items() if k != 'data-custom'} new_element = etree.Element(element.tag, {'data-custom': 'mycustomdata'} | old_attrib) # copy any other parts (input wouldn't have children, but then order shouldn't matter either) new_element[:] = element[:] new_element.text = element.text new_element.tail = element.tail # replace the original element.getparent().replace(element, new_element)

回答 1 投票 0

在 Python 中抓取需要输入和验证码的网页

我正在尝试抓取一个网站,该网站提供个人访问新泽西县法院法庭案件的权限。但我在弄清楚如何开始方面遇到了很多困难。我已经爬了很多网了...

回答 2 投票 0

我无法获取整页的 html 源代码

我试图使用 beautiful soup 学习网页抓取并尝试获取 html 文件,但它只有很少的内容,我不知道为什么,请帮助我。 导入请求 从 bs4 导入 BeautifulSoup

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.