以测试维基百科为例:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import unittest
class TestWikipedia(unittest.TestCase):
bsObj = None
def setUpClass():
global bsObj
url = 'http://en.wikipedia.org/wiki/Monty_Python'
bsObj = BeautifulSoup(urlopen(url))
print('setting up the test')

本文通过实例展示了如何使用Python爬虫对维基百科页面进行测试,包括检查页面标题是否为'Monty Python'及是否存在指定ID的div节点。测试中利用unittest的setUpClass方法确保页面内容只加载一次,提高了效率。
最低0.47元/天 解锁文章
436

被折叠的 条评论
为什么被折叠?



