python 爬虫(一)

news/2024/7/19 9:53:16 标签: python, 爬虫

1. 一次简单的网页访问

urllib 是一个标准的python库(意味着不需要安装任何附件的东西来运行这个demo),包含了通过网络请求数据的方法,处理cookies,甚至更改metadata比如headers和用户代理。

urlopen 这个方法用来通过网络访问远程数据,就是发送一个get请求到制定的url地址。

1 from urllib.request import urlopen
2 html = urlopen('http://pythonscraping.com/pages/page1.html')
3 print(html.read())
View Code

2. BeautifulSoup的简单介绍

    这个模块是一个html/xml解析器,它可以很好的处理不规范标记并生成剖析树。

   I  installing BeautifulSoup  

       linux:apt-get install python-bs4  或者 pip install beautifulsoup4

   II 在这个模块(bs4)中BeautifulSoup是最常用的

1 from urllib.request import urlopen
2 from bs4 import BeautifulSoup
3 
4 html = urlopen("http://www.pythonscraping.com/pages/page1.html")
5 bsobj = BeautifulSoup(html.read(), 'html.parser')
6 print(bsobj.h1)
7 
8 #输出:<h1>An Interesting Title</h1>
View Code

 3. 异常处理

   I 在服务上没有找到请求的地址 一个http error错误会被返回 这个错误可能是 404 page  not found 500 Internal server Error 这些所有的情况urlopen方法会抛出异常HTTPError

1 try:
2     html = urlopen("http://www.pythonscraping.com/exercises/exercise1.html")
3 except HTTPError as e:
4     print(e)
5      #return null,break or do some other 'plan B'
6 else:
7     #program continues

   II 服务没有找到,urlopen会返回None

1 if html is None:
2     print("URL is not found")
3 else:
4     #program continues

一个捕获各种异常的写法:

 1 from urllib.request import urlopen
 2 from urllib.error import HTTPError
 3 from bs4 import BeautifulSoup
 4 def getTitle(url):
 5   try:
 6     html = urlopen(url)
 7   except HTTPError as e:
 8     return None
 9   try:
10     bsObj = BeautifulSoup(html.read())
11     title = bsObj.body.h1
12   except AttributeError as e:
13     return None
14   return title
15 title = getTitle("http://www.pythonscraping.com/exercises/exercise1.html")
16 if title == None:
17   print("Title could not be found")
18 else:
19   print(title)

 

转载于:https://www.cnblogs.com/someoneHan/p/6224293.html


http://www.niftyadmin.cn/n/1065001.html

相关文章

css 滚动条样式_自定义滚动条样式「兼容所有浏览器」

1、友好的谷歌&#xff0c;几行css样式就可以搞定/* 滚动条样式 */.task-scrollbar{width:200px;height:300px;overflow-y:scrool;}.task-scrollbar::-webkit-scrollbar { /*滚动条整体样式*/ width : 10px; /*高宽分别对应横竖滚动条的尺寸*/ height: 1px; }.tas…

从测试角度度量项目质量的7个维度

“严重Bug”指的是在项目中&#xff0c;优先级为A和B的Bug。由于我们公司用的JIRA不像Bugzilla那样&#xff0c;对Bug分为“严重程度”和“优先级”两个维度&#xff0c;因此我们在报Bug时根据情况综合这两点的影响&#xff0c;统一以“优先级”来衡量Bug级别。A级Bug是指程序无…

Scut 上线后遇到的问题

1. 上线后的大并发问题&#xff1a; var sem new Semaphore(_accepts, _accepts);while (true){sem.WaitOne();#pragma warning disable 4014_listener.GetContextAsync().ContinueWith(async (t) >{try{sem.Release();var ctx await t;await ProcessListenerContext(ctx,…

Jscript setTimeOut用法

setTimeout(“GetFlag()”, 3000); //GetFlag() 是方法

vue router动态路由,路由懒加载

懒加载 在router中&#xff0c;Home和about的两种不同引入方式&#xff0c;About就是懒加载。 import Vue from "vue"; import VueRouter from "vue-router"; import Home from "../views/Home.vue"; Vue.use(VueRouter); const routes [{pat…

打开页面右下角出现弹窗效果

思路&#xff1a; 1.由于此弹窗是在登录系统的首次出现&#xff0c;固需要在系统的首页中添加&#xff0c;如这里的sysIndex.jsp中添加&#xff1b; 2.将下面的代码复制到jsp页面即可&#xff1b; 3.在此过程中出现弹窗随着主页右侧的下拉框的拉动而变形现象&#xff0c;解决办…

[算法] Manacher算法线性复杂度内求解最长回文子串

参考&#xff1a;http://www.felix021.com/blog/read.php?2040 以上参考的原文写得很好&#xff0c;解析的非常清楚。以下用我自己的理解&#xff0c;对关键部分算法进行简单的描述&#xff1a; 回文的判断需要完成从中心字符向两侧进行逐字符匹配&#xff1b;回文好比圆&…

asp.net后台控制div style

后台控制div样式显示或者隐藏&#xff1a; protected global::System.Web.UI.HtmlControls.HtmlGenericControl confirmation_new;//定义 ID this.confirmation_update.Style.Add("display", "block");// 直接用 做个备份 免的以后再用 转载于:https://www…