python 网页调试_Python爬虫和函数调试

一:函数调试

用之前学过的try···except进行调试

测试球赛的GameOver函数

defgameover(setA,setB):if setA==3 or setB==3:returnTrueelse:returnFalsetry:

a=gameover(3,4)print(a)except:print("Error")

输出True

二:Python爬虫

requests库是一个简洁且简单的处理HTTP请求的第三方库。

get()是对应与HTTP的GET方式,获取网页的最常用方法,可以增加timeout=n 参数,设定每次请求超时时间为n秒

text()是HTTP相应内容的字符串形式,即url对应的网页内容

content()是HTTP相应内容的二进制形式

用requests()打开搜狗主页20次

importrequeststry:for i in range(20):

r=requests.get("https://123.sogou.com",timeout=30)print(type(r))print(r.text)except:print('error')

三、

importrefrom bs4 importBeautifulSoup

html= """

菜鸟教程(runoob.com)

我的第一个标题

我的第一个段落。

row 1, cell 1row 1, cdll 2
row 2, cell 1row 2, cell 2

"""soup= BeautifulSoup(html,"html.parser")print(soup.prettify())print("(B).该html的body标签内容为\n{}".format(soup.body.prettify()))

该html的body标签内容为

我的第一个标题

我的第一个段落。

row 1, cell 1< d>

row 1, cdll 2< d>

< r>

row 2, cell 1< d>

row 2, cell 2< d>

< r>

< able>

&ltody>

<\html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值