《Python网络数据采集》读后总结 --第13、14章单元测试和远程抓取

浏览: 2755

第13章单元测试


介绍了基本的测试概念,如何使用Python unittest?

Python自带标准的测试模块unittest, 可以使用assert测试。

示例:

 1-wikiUnitTest.py
 2-wikiSeleniumTest.py
 3-interactiveTest.py
 4-dragAndDrop.py
 5-takeScreenshot.py
 6-combinedTest.py

示例2介绍了Selenium和网站做更多的交互测试,如何unittest看一下这个代码即可:

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver import ActionChains
import unittest
class TestAddition(unittest.TestCase):
    driver = None
    def setUp(self):
        global driver
        #driver = webdriver.PhantomJS(executable_path='<Path to Phantom JS>')
driver = webdriver.PhantomJS() #PATH路径下
        url = 'http://pythonscraping.com/pages/javascript/draggableDemo.html'
        driver.get(url)
    def tearDown(self):
        print("Tearing down the test")
    def test_drag(self):
        global driver
        element = driver.find_element_by_id("draggable")
        target = driver.find_element_by_id("div2")
        actions = ActionChains(driver)
        actions.drag_and_drop(element, target).perform()
        self.assertEqual("You are definitely not a bot!", driver.find_element_by_id("message").text)
if __name__ == '__main__':
    unittest.main()


第14章如何使用远程服务

介绍了必要性:block IP是网站防御的一个常用手段,如何避开,这个必须介绍。

最主要使用代理,使用The Onion Router network(Tor 洋葱路由),地址是https://www.torproject.org/download/download

这个我也没用过,先假设可以用吧,看介绍是把你所有的内容分发加密了。

使用这个的缺点就是慢啊。

使用方法 :

1.PySocks使用代理连接本地的Tor服务

#1-socks.py

import socks
import  socket
from urllib.request
import urlopensocks.set_default_proxy(socks.SOCKS5,"localhost",9150)
socket.socket=socks.socksocket
print(urlopen('http://icanhazip.com').read()

2.使用PhantomJS的代理服务

#2-seleniumSocks.py
from selenium import webdriver
service_args = [ '--proxy=localhost:9150', '--proxy-type=socks5', ]
driver = webdriver.PhantomJS(executable_path='<path to PhantomJS>', service_args=service_args)
driver.get("http://icanhazip.com")
print(driver.page_source)
driver.close()

还介绍了如何在Cloud上运行,这个具体大家看书吧。

书中最后的几句总结

我觉很不错,我就放在这里了。

What is the question I want answered, or the problem I want solved?

What data will help me achieve this, and where is it??

How is the website displaying this data? Can I identify exactly which part of thewebsite’s code contains this information?

How can I isolate the data and retrieve it?

What processing or analysis needs to be done to make it more useful?

How can I make this process better, faster, and more robust?

Internet的总结:

the Internet is one giant API with a somewhat poor userinterface.


我的总结:

终于把《Python网络数据采集》读后总结都弄完了,对自己帮助很大。

这本书主要是拓阔了我对相关工具的理解,其实对爬虫来说,原理不多,扩展的package和工具花费了蛮多时间,不过确实很有用。

接下来我还会把书中介绍的相关工具统一总结一下,然后会写一个Scraper的简单介绍,书中由于写的时候,Scrpaer  for Python 3的版本没出,都没怎么介绍

不过真的要应用,建议大家还是使用Scraper吧,书中的内容作为技术介绍即可,真的应用,好多没考虑了。

推荐 3
本文由 seng 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

2 个评论

seng老师的读书笔记值得学习
感谢认同, 大家一起多交流。

要回复文章请先登录注册