热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python爬取防爬虫网站的数据

对于反爬虫的网站,比如天眼查,使用phantomJS和selenium这两个可以很轻松的爬取出来举例来说,在天眼查中搜索百度,然后查看网页源代码,在源代码中查找的时候并不能查找到百度词条

对于反爬虫的网站,比如天眼查,使用phantomJS和selenium这两个可以很轻松的爬取出来


举例来说,在天眼查中搜索百度,然后查看网页源代码,在源代码中查找的时候并不能查找到百度词条,因为它是防爬虫的。


输入的如果是中文的字符串,要注意对中文字符串进行解码,转化成浏览器可以识别的网址形式,

代码如下:(爬取对应的公司名称)

#!/usr/bin/python
#coding: utf-8

from bs4 import BeautifulSoup
from selenium import webdriver
import urllib2

# Zip压缩包解压后exe文件所在的完整的位置
driver = webdriver.PhantomJS(executable_path= r"D:\phantomjs-2.1.1-windows\bin\phantomjs.exe")

def search(keyword):
# 将手动输入的字符串进行转码
keyword = keyword.encode("utf-8")
url_keyword = urllib2.quote(keyword)
url = "http://www.tianyancha.com/search?key=%s&checkFrom=searchBox" % url_keyword
# print(url)
driver.get(url)

soup = BeautifulSoup(driver.page_source, "lxml")
# print(soup)
soup = soup.find_all("span", {"class" : "ng-binding", "ng-bind-html" : "node.name | trustHtml"})

for s in soup:
# 输出文本的内容
print s.get_text()

if __name__ == "__main__":
while True:
x = raw_input(u"输入字符串:")
search(x)




推荐阅读
author-avatar
Young_阳先生
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有