python从web抓取信息

webbrowser模块:open()函数可以打开一个浏览器的指定url,这大概就是webbrowser模块唯一能做的事情了

import webbrowser
webbrowser.open("www.baidu.com")

request模块:

1> 不是python自带的模块,需要安装 pip install request

2> 编写request模块是因为python的urllib2模块用起来太复杂,当你需要从Web下载东西的时候使用request就好

import requests
res = requests.get("https://jingyan.baidu.com/article/2a138328efdb44074a134fc5.html")
print(type(res))
print(res.status_code == requests.codes.ok)
print(len(res.text))
print(res.text[:250])
=======================================================================================
result:
<class 'requests.models.Response'>
True
160814
<!DOCTYPE html><html><!--STATUS OK--><head><meta http-equiv="X-UA-Compatible" content="IE=Edge" /><meta charset="utf-8" /><meta name="referrer"

检查错误:在response对象上调用raise_for_status方法,如果下载文件出错,将会抛出异常,如果下载成功就什么都不做

import requests
res = requests.get("http://inventwithpyon.com/page_that_does_not_exist")
res.raise_for_status()

BeautifulSoup模块:

1> 用于从HTML页面提取信息

2> 模块名称bs4,需要引用的时候import bs4

html.example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>The Website Title</title>
</head>
<p>Download my<strong>Python</strong></p>
<p><span id="author">Super Yang</span></p>
</body>
</html>

main.py:

import bs4
exampleFile = open("example.html")
exampleSoup = bs4.BeautifulSoup(exampleFile.read())
elems = exampleSoup.select("#author")
print(len(elems))
print(elems[0].getText())
print(str(elems[0]))
print(elems[0].attrs)
print(elems[0].get('id'))
===================================================
result:
1
Super Yang
<span id="author">Super Yang</span>
{'id': 'author'}
author

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SuperYang_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值