如何使用 beautifulsoup 打印 href 属性,同时通过 selenium 实现自动化?

2023-12-04

blue element is what i want to access for web scrapping

蓝色元素的 href 值是我想从此 HTML 访问的内容

我尝试了几种方法来打印链接,但没有成功。

我的代码如下:-

discover_page = BeautifulSoup(r.text, 'html.parser')

finding_accounts = discover_page.find_all("a", class_="author track")
print(len(finding_accounts))

finding_accounts = discover_page.find_all('a[class="author track"]')
print(len(finding_accounts))

accounts = discover_page.select('a', {'class': 'author track'})['href']
print(len(accounts))

Output:- 
0
0
TypeError: 'dict' object is not callable

网页的网址是https://society6.com/discover但网址更改为https://society6.com/society?show=2登录我的帐户后

我在这里做错了什么?

注意:-我在这里使用 selenium chrome 浏览器。这里给出的答案在我的终端中有效,但在我运行文件时无效

我的完整代码:-

from selenium import webdriver
import time
import requests
from bs4 import BeautifulSoup
import lxml

driver = webdriver.Chrome()
driver.get("https://society6.com/login?done=/")
username = driver.find_element_by_id('email')
username.send_keys("[email protected]")
password = driver.find_element_by_id('password')
password.send_keys("sultan1997")
driver.find_element_by_name('login').click()

time.sleep(5)

driver.find_element_by_link_text('My Society').click()
driver.find_element_by_link_text('Discover').click()

time.sleep(5)

r = requests.get(driver.current_url)
r.raise_for_status()

'''discover_page = BeautifulSoup(r.html.raw_html, 'html.parser')

finding_accounts = discover_page.find_all("a", class_="author track")
print(len(finding_accounts))

finding_accounts = discover_page.find_all('a[class="author track"]')
print(len(finding_accounts))


links = []
for a in discover_page.find_all('a', class_ = 'author track'): 
        links.append(a['href'])
        #links.append(a.get('href'))

print(links)'''

#discover_page.find_all('a')

links = []
for a in discover_page.find_all("a", attrs = {"class": "author track"}): 
        links.append(a['href'])
        #links.append(a.get('href'))

print(links)

#soup.find_all("a", attrs = {"class": "author track"})'''

soup = BeautifulSoup(r.content, "lxml")
a_tags = soup.find_all("a", attrs={"class": "author track"})

for a in soup.find_all('a',{'class':'author track'}):
    print('https://society6.com'+a['href'])

文档中的代码是我正在尝试使用的代码


如果您希望找到所有链接而不需要在 Beautifulsoup 中手动尝试。然后去请求-html

获取所有链接的示例代码,

from requests_html import HTMLSession
from bs4 import BeautifulSoup

url = 'https://society6.com/discover'
session = HTMLSession(mock_browser=True)
r = session.get(url, headers={'User-Agent': 'Mozilla/5.0'})

print(r.html.links)
print(r.html.absolute_links)

soup = BeautifulSoup(r.html.raw_html, 'html.parser')
a_tags = soup.find_all("a", attrs={"class": "author track"})
for a_tag in a_tags:
    print(a_tag['href'])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 beautifulsoup 打印 href 属性,同时通过 selenium 实现自动化? 的相关文章

随机推荐

  • MapView 上的 java.util.ConcurrentModificationException

    fellas 很多天以来我都面临着非常奇怪的问题 我正在尝试经常更新覆盖 因此 有时当我触摸地图时 我会收到 java util ConcurrentModificationException 或者有时当地图尝试更新叠加层时 我会收到 ja
  • 如何使用 Json.NET 序列化和反序列化数组的 ArrayList

    我需要在应用程序中使用 JSON NET 将对象序列化 反序列化为 json Object 具有 ArrayList 属性类型 其中包含字符串数组 为了模拟它 我编写了以下单元测试 public class JsonTests public
  • 仅在释放模式下按 HOME 时才会终止 Activity

    首先 我有两个 Activity Splash 和 MainActivity 仅支持纵向 在 MainActivity 中 我有很多片段使用幻灯片菜单 我想在用户离开 MainActivity 时保留当前片段 这是我的尝试 int curr
  • 无法在 CodeIgniter 中使用会话类检索会话 ID

    我的控制器中有这段代码 class Upload center extends Controller function construct parent Controller this gt load gt model auth model
  • 在 Automator 中编辑文本

    我想创建一个 Automator 服务 它可以获取所选文本并将其替换为编辑后的版本 i e 所选文本将类似于 这是所选文本 然后输出将采用该文本并输出类似 一些预定义的开始 这是选定的文本 这是选定的文本 我以前曾对文件名做过类似的事情 但
  • 如何使用 PHP Bitly v4 缩短 URL?

    我有 Bitly 的代码v3并且运行良好
  • RedirectToAction 不是重定向

    这不应该起作用吗 如果我最后有一个断点 它停在那里 但永远不会到达ContactAction 发布后的页面只是一个空白页面 没有源代码 我错过了什么 谢谢 您的联系人 ContactModel 模型 不应为 void 而应为 public
  • 如何将数据从应用程序发送到AppWidgetProvider?

    我陷入了一个特定的场景 用户从应用程序更新时间后 我需要立即更新我的小部件 我确实尝试通过 Intent Extras 发送数据来进行广播 但失败了 目前 我的数据在AppWidgetProvider我需要将此数据发送到服务 public
  • 更改 LISP 中列表的副本

    在 LISP 中 我有一个传递列表的函数 我想更改此列表的元素而不更改原始列表 通常情况下 我会使用copy list创建我将更改的列表的本地副本 但这似乎不起作用 CL USER gt defun test item let copy c
  • Azure DevOps 多阶段管道陷入等待批准状态

    我将托管的 Azure DevOps 与 Azure Git Repos 中的代码一起使用 我们曾经使用 经典 的基于 UI 的管道编辑器 但在构建 发布阶段正在转向 YAML 模板 过去 我配置了 CI CD 以便当代码通过拉取请求提交到
  • 类型“List”不是类型“List”的子类型

    我有一段从 Firestore 示例中复制的代码片段 Widget buildBody BuildContext context return new StreamBuilder stream getEventStream builder
  • 更新 Entity Framework 6 中的子对象

    使用实体框架6 2 我有以下两个模型 实体 public class City public int CityId get set public string Name get set public class Country public
  • Rails 如何获得最佳性能请求并行 sidekiq 工作线程

    我的 Rails 应用程序有一名 sidekiq 工作人员 该工作线程将向外部 api 发出 2500 个请求 响应是一个 xml 如何让该员工获得最佳绩效 在工作线程内部 生成应用程序级线程 例如 创建 10 个 ruby 线程来处理 2
  • 如何用 Python 编写与 Wikipedia 中的示例不同的策略模式?

    在 2009 年策略模式的维基百科条目中 有一个例子用 PHP 编写 大多数其他代码示例都会执行以下操作 a Context new StrategyA new a execute gt Doing the task the normal
  • 在 JavaScript 中检查字母数字的最佳方法

    对文件执行字母数字检查的最佳方法是什么INPUT领域在JSP 我已附上我当前的代码 function validateCode var TCode document getElementById TCode value for var i
  • Form_Load() '事件' 或覆盖 OnLoad()

    我希望有人尝试解释它们之间的区别 更具体地说 是示例使用场景 我正在重构一些窗口窗体代码和一个Form有一些代码在Form Load 事件并且也在protected override void OnLoad 调用的事件base OnLoad
  • @Dependent 范围在 Wildfly 中不是默认的吗?

    我在使用 Wildfly 通过 Inject 注入 POJO 时遇到一些麻烦 文档明确指出 Dependent 如果未指定 则默认范围 这意味着一个对象的存在只是为一个客户端 bean 提供服务 并且与该客户端 bean 具有相同的生命周期
  • 如何在Python线程中使用qtwebkit?

    我正在尝试使用 qtwebkit 解析 js 生成的网页 我找到了如何获取页面源的示例 import sys from PySide QtGui import from PySide QtCore import from PySide Qt
  • 如何测试更新方法?

    我是单元测试新手 并在我的 Java Spring Boot 应用程序中使用 JUnit 我有时需要测试更新方法 但是当我在网上搜索时 没有合适的示例或建议 那么 您能否澄清一下如何测试以下更新方法 我认为这可能需要与测试 void 不同的
  • 如何使用 beautifulsoup 打印 href 属性,同时通过 selenium 实现自动化?

    蓝色元素的 href 值是我想从此 HTML 访问的内容 我尝试了几种方法来打印链接 但没有成功 我的代码如下 discover page BeautifulSoup r text html parser finding accounts