动态网页抓取(介绍使用BeautifulSoup抓取静态网页,使用Selenium-WebDriver抓取动态网页 )
优采云 发布时间: 2022-04-03 19:12动态网页抓取(介绍使用BeautifulSoup抓取静态网页,使用Selenium-WebDriver抓取动态网页
)
介绍使用Beautiful Soup爬取静态网页和使用Selenium-WebDriver爬取动态网页
# 环境
import requests
# post body
body = {
"ck": '',
"area_code": "+86",
"number": "18797811992",
"analytics": "analytics_log"
}
# 请求头
headers = {
"Host": "accounts.douban.com",
"Origin": "https://accounts.douban.com",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "https://accounts.douban.com/passport/login_popup?login_source=anony",
"Accept-Language": "zh,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7",
"Cache-Control": "no-cache",
"Connection": "keep-alive"
}
# URL参数
params = {
"k": "v",
"k2": "v2"
}
# 发送POST请求,设置请求方式,请求地址,请求体,请求头,请求参数,超时时间(单位为秒)
response = requests.post("https://accounts.douban.com/j/mobile/login/request_phone_code", data=body, headers=headers,
params=params, timeout=1)
# 相应的编码
print(response.encoding)
# 响应的状态
print(response.status_code)
# 响应的内容,根据头部字符编码进行解码
print(response.text)
# 响应的二进制形式,如读取文件
print(response.content)
# 将响应解析为json
print(response.json())
Beautiful Soup 是一个 Python 库,用于从 HTML 或 XML 文件中提取数据。它支持通过您最喜欢的转换器导航、查找和修改文档的惯用方式。Beautiful Soup 将为您节省数小时甚至数天的营业时间。
#爬虫演示#静态网络爬虫演示:
import requests
from bs4 import BeautifulSoup
# 请求URL
response = requests.get("http://www.santostang.com")
# 将响应传给bs4
bs = BeautifulSoup(response.text, "html.parser")
# 查找所有class为`article-list-1 clearfix`的article元素
article_list = bs.find_all("article", class_="article-list-1 clearfix")
# 将文本写入到csv文件中
with open("article_title.csv", "a") as article_file:
for article in article_list:
# 打印获取到的文本
title = article.header.h1.a.text
print(title)
# 写入
article_file.write(title + ",")
article_file.write("\n")
# 抓取动态网页
与静态网页相比,动态网页的内容不会出现在 HTML 源代码中。所以需要使用动态网页抓取技术:使用 Selenium-webDriver 模拟浏览器行为
Selenium 有许*敏*感*词*,但其核心是一个用于 Web 浏览器自动化的工具集,它使用最好的技术来远程控制浏览器实例并模拟用户与浏览器的交互。
下载对应的驱动,根据浏览器和操作系统进行配置:
图像.png
网元
一个 WebElement 代表一个 DOM 元素。可以通过使用 WebDriver 实例从文档根节点搜索或通过在另一个 WebElement 下搜索来找到 WebElement。
WebDriver API 提供内置方法来根据不同的属性(例如 ID、名称、类、XPath、CSS 选择器、链接文本等)查找 WebElement。
from selenium.webdriver import Chrome
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# 实例化驱动程序,并指定驱动程序地址(默认也会取PATH中配的值)
chrome_driver = Chrome(executable_path="/opt/WebDriver/bin/chromedriver")
try:
url = "http://www.baidu.com"
# 最大化窗口
chrome_driver.maximize_window()
# 打开网站
chrome_driver.get(url)
print("当前的URL为:" + chrome_driver.current_url)
# 等待DOM渲染完成
# WebDriverWait(chrome_driver).until()
# 获取input输入框元素
input_el = chrome_driver.find_element_by_id("kw")
# 在输入框输入文本
input_el.send_keys("Selenium WebDriver自动输入")
# 获取`百度一下`按钮
search_btn = chrome_driver.find_element_by_id("su")
# 模拟点击事件
search_btn.click()
# 这时候已经到另外一个页面了===========
# 搜索结果页的输入框
search_result = chrome_driver.find_element_by_id("kw")
# 清除数据
search_result.clear()
# 等待网页渲染完成,不然可能导致无法查询到想要获取的元素的异常
WebDriverWait(chrome_driver, 5, 0.2).until(EC.presence_of_all_elements_located((By.TAG_NAME, "a")))
WebDriverWait(chrome_driver, 5, 0.2).until(EC.presence_of_all_elements_located((By.ID, "1")))
# 第一条搜索结果
result_item_1 = chrome_driver.find_element_by_id("1")
a_link = result_item_1.find_element_by_tag_name("h3").find_element_by_tag_name("a")
# 根据a标签的href跳转到新的网页
a_link_href = a_link.get_property("href")
print("即将跳转的链接地址为:" + a_link_href)
chrome_driver.get(a_link_href)
# 刷新
chrome_driver.refresh()
# 屏幕截图
chrome_driver.save_screenshot("./屏幕截图.png")
finally:
# 退出浏览器
chrome_driver.quit()