当前位置: 移动技术网 > IT编程>脚本编程>Python > Python Linux中用火狐无头浏览器爬取网页内容

Python Linux中用火狐无头浏览器爬取网页内容

2020年07月11日  | 移动技术网IT编程  | 我要评论

需要的包:

pip install selenium
pip install lxml
pip install bs4

本来一开始想用谷歌无头浏览器的,结果运行的时候一堆bug,换成火狐之后一下子就好了
安装firefox:

yum install firefox

驱动下载地址

https://github.com/mozilla/geckodriver

解压后我放在了/usr/bin下,放这似乎不用指定路径,同时为其添加可执行属性

from selenium import webdriver
from bs4 import BeautifulSoup 
options = webdriver.FirefoxOptions()
options.add_argument('-headless')

# 不指定路径
browser = webdriver.Firefox(options=options)
# 指定路径,如果用上一句不行的话就用下面的指定下路径
# browser = webdriver.Firefox(executable_path="/usr/bin/geckodriver",options=options)

browser.get("http://www.chinapeace.org.cn/gupiao/")
content = browser.page_source
soup = BeautifulSoup(content,'lxml')
a_docs = soup.find_all('a')
file = open('html.html','a')
for a_doc in a_docs:
    print a_doc
    print a_doc.get('href')
    print a_doc.string
    file.write(a_doc.encode('utf-8'))

在这里插入图片描述

本文地址:https://blog.csdn.net/qq_39664250/article/details/107251226

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网