当前位置: 首页 > wzjs >正文

上海企业营销型网站建设深圳竞价托管

上海企业营销型网站建设,深圳竞价托管,做网站开发的集团,广州天河网站建设这篇文章锁定官网教程中 Examples 章节中的 Web Browser Automation with Agents文章,主要介绍了如何设计一个由Agent驱动结合视觉模态的Web内容浏览功能,包含了以下几个功能: Navigate to web pages:前往指定网页;Cl…

这篇文章锁定官网教程中 Examples 章节中的 Web Browser Automation with Agents文章,主要介绍了如何设计一个由Agent驱动结合视觉模态的Web内容浏览功能,包含了以下几个功能:

  1. Navigate to web pages:前往指定网页;
  2. Click on elements:点击网页对象;
  3. Search within pages:在页面中搜索;
  4. Handle popups and modals:处理页面弹窗内容;
  5. Extract information :抽取信息;
  • 官网链接:https://huggingface.co/docs/smolagents/v1.9.2/en/examples/web_browser;

安装以下依赖:

$ pip install smolagents selenium helium pillow -q

为了实现上面这些功能,需要完成以下步骤:

  1. 定义能够对网页进行操作的 tool,包括可以执行 Ctrl+F、后退、关闭弹窗的功能;
  2. 配置浏览器内核,官网示例中使用了 Chrmoe 浏览器内核;
  3. 定义Agent和模型;
  4. 明确操作提示词;
  5. Agnet执行操作提示词;

完整代码如下:

【注意】:官网示例中使用的是 meta-llama/Llama-3.3-70B-Instruct 模型,但这个模型的Token是需要购买的,如果这里对其进行修改像之前文章中一样使用默认分配的 Qwen-Coder 那么会在中间某一步停下来,因为默认的免费模型不支持超过 10000 Token 的输入,有条件的读者可以尝试购买一些Token实验其完整功能。

from io import BytesIO
from time import sleepimport helium
from dotenv import load_dotenv
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keysfrom smolagents import CodeAgent, tool
from smolagents.agents import ActionStep
from smolagents import HfApiModelload_dotenv()#----------------------------------------------------------------# 
# Step1. 定义网页操作tool
@tool
def search_item_ctrl_f(text: str, nth_result: int = 1) -> str:"""Searches for text on the current page via Ctrl + F and jumps to the nth occurrence.Args:text: The text to search fornth_result: Which occurrence to jump to (default: 1)"""elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{text}')]")if nth_result > len(elements):raise Exception(f"Match n°{nth_result} not found (only {len(elements)} matches found)")result = f"Found {len(elements)} matches for '{text}'."elem = elements[nth_result - 1]driver.execute_script("arguments[0].scrollIntoView(true);", elem)result += f"Focused on element {nth_result} of {len(elements)}"return result@tool
def go_back() -> None:"""Goes back to previous page."""driver.back()@tool
def close_popups() -> str:"""Closes any visible modal or pop-up on the page. Use this to dismiss pop-up windows!This does not work on cookie consent banners."""webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()#----------------------------------------------------------------# 
# Step2. 配置Chrome内核# Configure Chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--force-device-scale-factor=1")
chrome_options.add_argument("--window-size=1000,1350")
chrome_options.add_argument("--disable-pdf-viewer")
chrome_options.add_argument("--window-position=0,0")# Initialize the browser
driver = helium.start_chrome(headless=False, options=chrome_options)# Set up screenshot callback
def save_screenshot(memory_step: ActionStep, agent: CodeAgent) -> None:sleep(1.0)  # Let JavaScript animations happen before taking the screenshotdriver = helium.get_driver()current_step = memory_step.step_numberif driver is not None:for previous_memory_step in agent.memory.steps:  # Remove previous screenshots for lean processingif isinstance(previous_memory_step, ActionStep) and previous_memory_step.step_number <= current_step - 2:previous_memory_step.observations_images = Nonepng_bytes = driver.get_screenshot_as_png()image = Image.open(BytesIO(png_bytes))print(f"Captured a browser screenshot: {image.size} pixels")memory_step.observations_images = [image.copy()]  # Create a copy to ensure it persists# Update observations with current URLurl_info = f"Current url: {driver.current_url}"memory_step.observations = (url_info if memory_step.observations is None else memory_step.observations + "\n" + url_info)#----------------------------------------------------------------# 
# Step3. 定义 Agent# Initialize the model
# 如果你有下面这个模型的Token则使用下面这两行代码
# model_id = "meta-llama/Llama-3.3-70B-Instruct"
# model = HfApiModel(model_id)
# 如果你只有免费的Token则使用下面这一行代码
model = HfApiModel()# Create the agent
agent = CodeAgent(tools=[go_back, close_popups, search_item_ctrl_f],model=model,additional_authorized_imports=["helium"],step_callbacks=[save_screenshot],max_steps=20,verbosity_level=2,
)# Import helium for the agent
agent.python_executor("from helium import *", agent.state)#----------------------------------------------------------------# 
# Step4. 明确操作提示词helium_instructions = """
You can use helium to access websites. Don't bother about the helium driver, it's already managed.
We've already ran "from helium import *"
Then you can go to pages!
Code:
```py
go_to('github.com/trending')
```<end_code>You can directly click clickable elements by inputting the text that appears on them.
Code:
```py
click("Top products")
```<end_code>If it's a link:
Code:
```py
click(Link("Top products"))
```<end_code>If you try to interact with an element and it's not found, you'll get a LookupError.
In general stop your action after each button click to see what happens on your screenshot.
Never try to login in a page.To scroll up or down, use scroll_down or scroll_up with as an argument the number of pixels to scroll from.
Code:
```py
scroll_down(num_pixels=1200) # This will scroll one viewport down
```<end_code>When you have pop-ups with a cross icon to close, don't try to click the close icon by finding its element or targeting an 'X' element (this most often fails).
Just use your built-in tool `close_popups` to close them:
Code:
```py
close_popups()
```<end_code>You can use .exists() to check for the existence of an element. For example:
Code:
```py
if Text('Accept cookies?').exists():click('I accept')
```<end_code>
"""search_request = """
Please navigate to https://en.wikipedia.org/wiki/Chicago and give me a sentence containing the word "1992" that mentions a construction accident.
"""#----------------------------------------------------------------# 
# Step5. Agent执行提示词
agent_output = agent.run(search_request + helium_instructions)
print("Final output:")
print(agent_output)

这里使用免费的Token执行结果如下,Agent会卡在中间的一步中,这个完全随缘,有时候刚打开网页还没有滚动就报错Token超限,有时候能滚动很多次才报错:

$ python demo.py

在这里插入图片描述

http://www.dtcms.com/wzjs/359326.html

相关文章:

  • 做水暖的网站百度快照怎么用
  • 中国建设安全监理协会网站热搜榜排名今日第一
  • 济南烨铭网站建设网站建设网络推广公司
  • 网站建设 网站优化营销型网站建设专家合肥seo公司
  • 网站怎么做才算精致关键词排名优化软件价格
  • 淄博网站建设排行榜淄博网站制作
  • 怎么用自己主机做网站_象山关键词seo排名
  • 商洛网站建设公司百度图片搜索入口
  • dreamweaver8可以做资源下载网站推广网站制作
  • 企业公司网站建设ppt推广平台都有哪些
  • 一般做网站什么价格百度竞价渠道代理
  • 毕业设计做网站用什么百度服务商
  • 做的网站百度上可以搜到吗淘宝运营培训多少钱
  • 新闻类wordpress模板下载今日头条搜索优化怎么做
  • 贵州住房和城乡建设委员会网站优化设计高中
  • 市直部门网站建设方案百度推广运营公司
  • 济南做网站优化哪家好近期国内热点新闻事件
  • 个人网站备案费用郑州做网站
  • 强 一级二级2022深圳seo公司
  • 商务网站建设设计结构内容广东seo网站设计
  • wordpress授权协议沈阳网站关键词优化多少钱
  • wordpress 网站加密市场调研方案
  • 东莞食品网站建设seo视频教程百度网盘
  • 怎么给网站做超链接最新一周新闻
  • wordpress 中文乱码青岛官网seo
  • 做b2b网站项目技巧长沙官网seo分析
  • 网站设计总结与心得体会网络推广营销
  • 武汉做网站的百度竞价课程
  • 彩票投资理财平台网站建设制作网站需要什么技术
  • 阿里巴巴运营流程网站seo课设