admin管理员组

文章数量:1616429

背景:通过python中直接get或者urlopen打开一些有延迟加载数据的网页,会抓取不到部分信息。

1. 命令行打开chrome,并开启调试端口(前提,找到chrome安装目录,找到chrome.exe所在路径,添加到环境变量中,例如我的是C:\Program Files\Google\Chrome\Application)

chrome.exe --remote-debugging-port=9527 --user-data-dir="D:\test"

remote-debugging-port指定远程调试端口(python调用的时候要用),user-data-dir指定用户数据目录,后续浏览器窗口关闭后,再次通过相同命令打开,则之前的登录信息等内容都还在,不用重复登录。可以通过这个方式浏览一些学习知识的网站(懂的都懂),但是不在浏览器留下历史记录(其实是留下了,只不过相当于在另一个浏览器里面了,所有相关数据都在指定的那个用户数据目录了)。
2. 下载浏览器驱动
不同浏览器有不同驱动,chrome下载地址为:http://chromedriver.storage.googleapis/index.html,或者这个https://registry.npmmirror/binary.html?path=chromedriver/,查看自己浏览器版本号,下载对应版本的驱动程序。
IE浏览器
说明:该浏览器为Windows系统自带,一般无需额外下载。
浏览器下载地址:https://www.microsoft/zh-cn/download/internet-explorer.aspx
驱动器下载地址:http://selenium-release.storage.googleapis/index.html

Microsoft Edge浏览器
浏览器下载地址:https://www.microsoft/zh-cn/edge
驱动器下载地址:https://developer.microsoft/zh-cn/microsoft-edge/tools/webdriver

Chrome(google)浏览器
浏览器下载地址:https://www.google/chrome
驱动器下载地址:http://chromedriver.storage.googleapis/index.html

Firefox(火狐)浏览器
浏览器下载地址:http://www.firefox
驱动器下载地址:https://github/mozilla/geckodriver/releases

Opera浏览器
浏览器下载地址:https://www.opera/zh-cn
驱动器下载地址:https://github/operasoftware/operachromiumdriver/releases

Safari浏览器
浏览器下载地址:https://www.apple/safari
驱动器说明:该浏览器无需下载驱动,可以直接执行代码。
3. 通过python代码打开指定网页
前提:pip install selenium

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from bs4 import BeautifulSoup
options = Op

本文标签: 浏览器网页内容信息Python