python动态网页爬虫

python动态网页爬虫

在用python爬取动态网页的时候,有些网页的HTML代码是由javascript动态生成的,直接爬取可能会出现无法加载的情况,需要用phantomJS和selenium模拟浏览器,之后再爬取。

安装准备

一.下载phantomJS压缩包,解压,注意路径./bin/phantomjs.exe,里面有各种浏览器的驱动。

二.pip install selenium安装selenium

例子
#爬取javacodeexample网站某网页中的url列表

from bs4 import BeautifulSoup
from selenium import webdriver
import time
url = 'https://www.programcreek.com/java-api-examples/?ClassName=lucene&action=search&submit=Search'
sleep_time = 10

#参数为phantomjs压缩包下./bin/phantomjs.exe的路径
driver = webdriver.PhantomJS(executable_path=r'E:\Downloads\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe')

driver.set_window_size(1920, 1080)
driver.get(url)
time.sleep(sleep_time)
page = BeautifulSoup(driver.page_source)
result=page.find_all("div",attrs={"id":"api-list-apiname"})
f = open('lucene-url.txt', 'w')
for res in result:
    re = res.find('a')
    f.writelines(re['href'])
    f.writelines("\n")
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值