python如何爬取搜索引擎的搜索数据?
网友回复
可以使用selenium爬取搜索引擎的搜索结果,我们以百度为例,这是运行的结果
#!/usr/local/python3/bin/python3 # -*- coding: utf-8 -* from selenium import webdriver from selenium.webdriver.common.keys import Keys from time import sleep from bs4 import BeautifulSoup from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC chromeOptions = webdriver.ChromeOptions() chromeOptions.add_argument("--headless") chromeOptions.add_argument("--remote-debugging-port=9222") chromeOptions.add_argument('--no-sandbox') browser = webdriver.Chrome('/usr/bin/chromedriver',chrome_options=chromeOptions) browser.get("https://www.baidu.com/") #进入相关网站 #保存网站截屏图片 browser.find_element_by_id('kw').send_keys('ai发展前景 ',Keys.RETURN) # 输入框 browser.find_element_by_id('su').click() # 点击搜索按钮 try: sleep(1) # 等待<h3 class="c-title t t tts-title">标签出现 # element = WebDriverWait(browser, 10).until( # EC.presence_of_element_located((By.CSS_SELECTOR, "h3.c-title.t.tts-title")) # ) # 获取网页内容 html_content = browser.page_source # 使用BeautifulSoup解析网页内容 soup = BeautifulSoup(html_content, 'html.parser') # 查找所有的<h3>标签 h3_tags = soup.find_all('h3', class_='c-title t t tts-title') # 提取<a>链接和标题 for h3 in h3_tags: a_tag = h3.find('a') if a_tag: link = a_tag['href'] title = a_tag.get_text(strip=True) print(f'Link: {link}') print(f'Title: {title}') print('-' * 50) # 关闭WebDriver browser.quit() finally: # 关闭浏览器 browser.quit()运行完后,我们还要解析清晰获取每个链接中的正文,然后汇总起来,我们可以通过以下这个python代码获取:
点击查看全文
python如何压缩视频到指定的分辨率大小?
抖音微信小程序有压缩视频分辨率的api吗?
ollama中的deepseek有没有本地越狱提示词?
deepseek r1各个版本本地电脑部署运行的硬件要求是多少?
python如何分离视频中的音频与视频为mp4与wav文件?
python报错: ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the ‘ssl‘ module is comp
ps支持命令行执行脚本文件修改图片吗?
传统的个人博客站长如何应对ai搜索的流量锐减?
centos安装python3.10提示ssl模块不存在?
deepseek r1如何使用本地gpu运行?