云主机下搭建网页爬虫

1. 相关组件

OS:腾讯云Ubuntu

浏览器:chrome + chromedriver

python爬虫框架: selenium

2.安装部署

2.1 安装chrome

sudo apt-get install libxss1 libappindicator1 libindicator7

wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

sudo apt install ./google-chrome*.deb

google-chrome-stable --version

google-chrome  --headless --disable-gpu  https://www.baidu.com/

2.2 安装chromedriver

wget https://storage.googleapis.com/chrome-for-testing-public/123.0.6312.58/linux64/chromedriver-linux64.zip

unzip chromedriver-linux64.zip

cp chromedriver /usr/local/bin/

2.3 安装python3

2.4 安装selenium

pip install selenium

2.5 编写爬虫程序

from pyvirtualdisplay import Display
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')#无头模式,服务器没有图形界面这个必须
chrome_options.add_argument('--disable-gpu')#不需要gpu加速
chrome_options.add_argument('--no-sandbox') # 这个配置很重要
#s = Service('/usr/local/bin/chromedriver')
client = webdriver.Chrome(options=chrome_options)    # 如果没有把chromedriver加入到PATH中,就需要指明路径

#client.get("https://ec.chng.com.cn/channel/home/?SlJfApAfmEBp=1704119704734#/")
#client.get("https://ec.chng.com.cn/channel/home/?SlJfApAfmEBp=1704119704734#/detail?id=12542364")
client.get("https://www.zhihu.com/question/553813879/answer/3440060321")
print (client.page_source.encode('utf-8'))
 
client.quit()

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值