如何清理爬虫数据分析中一些不需要的 HTML 属性

后使用快捷导航没有帐号?
Java爬虫的一些总结和心得
查看: 41291|
评论: 0|来自: CSDN
摘要: 关于Java爬虫的一些总结和心得,最近做了很多关于爬虫到项目,写点感想,以后查询1.请求http连接,并保存内容,catch不同到exception进行反爬处理,我一般采用正则匹配,这比较适用于爬取数据不多,网站只返回HTML内 ...
最近做了很多关于爬虫到项目,写点感想,以后查询1.请求http连接,并保存内容,catch不同到exception进行反爬处理int&countUrl=0;&&&&&&&&&&&&public&String&getOneHtml(String&htmlurl,String&encoding,String&cookie)&throws&IOException,&InterruptedException&&&&&&{&&int countUrl=0;
public String getOneHtml(String htmlurl,String encoding,String cookie) throws IOException, InterruptedException
{[java] &&&&&&&&&&&&&&&&&&&&&if(countUrl==5){&&&&&&&&&&countUrl=0;&&&&&&&&&&return&"0";&&&&&&}&&&&&&&&&&&&&&String&&&final&StringBuffer&sb&=&new&StringBuffer();&&HttpURLConnection&httpConn&=&null;&&try&&{&&&&&&URL&url&=&new&URL(htmlurl);&&&&&&&&&&&&httpConn&=&(HttpURLConnection)&url.openConnection();&&&&&&&&
//最多重复请求5次,用来反爬的
if(countUrl==5){
countUrl=0;
return "0";
//System.out.println(cookie);
final StringBuffer sb = new StringBuffer();
HttpURLConnection httpConn =
URL url = new URL(htmlurl);
httpConn = (HttpURLConnection) url.openConnection();
//头设置,get方法[java] &&&&&&&HttpURLConnection.setFollowRedirects(true);&&&&&&&&&httpConn.setRequestMethod("GET");&&&&&&&&&httpConn.setRequestProperty("User-Agent","Mozilla/5.0&(X11;&&x86_64)&AppleWebKit/537.36&(KHTML,&like&Gecko)&Chrome/33.0.&Safari/537.36");&&&&&&&&&httpConn.setRequestProperty("Connection","keep-alive");&&&&&&&&&httpConn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml");&&&&&&&&&httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");&&&&&&&&&httpConn.setRequestProperty("cookie",cookie);&&&&&&&&&httpConn.setRequestProperty("Cache-control","no-cache,&no-store");&&&&&&&&&&httpConn.setRequestProperty("Host","www.linkedin.com");&&&&&&&&&httpConn.setConnectTimeout(20000);&&&&&&&&&httpConn.setReadTimeout(20000);&&&&&&&&&&&&&&&&BufferedReader&in&=&new&BufferedReader(new&InputStreamReader(httpConn.getInputStream(),&encoding));&&&&&if(httpConn.getResponseCode()!=200){&&&&&&&&&&&&&&&&&&&&&&&&&&httpConn.disconnect();&&&&&&&&&Thread.sleep(30000);&&&&&&&&&&&cookie=login();&&&&&&&&&return&getOneHtml(htmlurl,encoding,cookie);&&&&&}&&&&&&while&((temp&=&in.readLine())&!=&null)&&&&&&&&&&&&&&&{&&&&&&&&&temp=temp.replaceAll("&&&","");&&&&&&&&&temp=temp.replaceAll("u002d","-");&&&&&&&&&temp=temp.replaceAll("u0026","&");&&&&&&&&&temp=temp.replaceAll("u002d","-");&&&&&&&&&temp=temp.replaceAll("u0026","&");&&&&&&&&&temp=temp.replaceAll("n","");&&&&&&&&&temp=temp.replaceAll("t","");&&&&&&&&&temp=temp.replaceAll("r","");&&&&&&&&&sb.append(temp);&&&&&}&&&&&in.close();&&&&&httpConn.disconnect();&&&&&}&&catch&(final&MalformedURLException&me)&&{&&&&&System.out.println("url不存在!");&&&&&me.getMessage();&&&&&throw&&&}&&catch&(final&FileNotFoundException&me)&&{&&&&&&System.out.println(htmlurl+"反爬启动");&&&&&return&"0";&&}&&catch&(final&IOException&e)&&{&&&&&&e.printStackTrace();&&&&&&System.out.println("反爬启动:"+htmlurl+"次数:"+countUrl++);&&&&&&httpConn.disconnect();&&&&&&Thread.sleep(20000);&&&&&&return&this.getOneHtml(htmlurl,&encoding,cookie);&&}&&&&&&&&&&&&&&&&&&countUrl=0;&&&&&&httpConn.disconnect();&&&&&&&&&&&&return&sb.toString();&&&&&&&&}&&
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0. Safari/537.36");
httpConn.setRequestProperty("Connection","keep-alive");
httpConn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml");
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setRequestProperty("cookie",cookie);
httpConn.setRequestProperty("Cache-control","no-cache, no-store");
httpConn.setRequestProperty("Host","www.linkedin.com");
httpConn.setConnectTimeout(20000);
httpConn.setReadTimeout(20000);
// logger.info(httpConn.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), encoding));////打开连接,获取内容
if(httpConn.getResponseCode()!=200){
//System.out.println(httpConn.getHeaderField("location"));
// System.out.println(httpConn.getResponseCode()+htmlurl);
httpConn.disconnect();
Thread.sleep(30000);
cookie=login();
return getOneHtml(htmlurl,encoding,cookie);
while ((temp = in.readLine()) != null)
//替换点一些无用到符号
temp=temp.replaceAll(" ","");
temp=temp.replaceAll("u002d","-");
temp=temp.replaceAll("u0026","&");
temp=temp.replaceAll("u002d","-");
temp=temp.replaceAll("u0026","&");
temp=temp.replaceAll("n","");
temp=temp.replaceAll("t","");
temp=temp.replaceAll("r","");
sb.append(temp);
in.close();
httpConn.disconnect();
catch (final MalformedURLException me)
System.out.println("url不存在!");
me.getMessage();
catch (final FileNotFoundException me)
System.out.println(htmlurl+"反爬启动");
return "0";
catch (final IOException e)
e.printStackTrace();
System.out.println("反爬启动:"+htmlurl+"次数:"+countUrl++);
httpConn.disconnect();
Thread.sleep(20000);
return this.getOneHtml(htmlurl, encoding,cookie);
//System.out.println(sb);
countUrl=0;
httpConn.disconnect();
return sb.toString();
}[java] 2.模拟登录,获取cookie:&&<pre class="java" style="display:" name="code" snippet_file_name="blog__2426797" code_snippet_id=".模拟登录,获取cookie:[java] &&[java] public&String&login()&throws&MalformedURLException,&InterruptedException{&&&&&&&&&&&&&&&&&&&&String&htmlurl="https://www.linkedin.com/uas/login-submit";&&&&&&&&&&HttpURLConnection&httpConn&=&null;&&&&&&&&&&String&cookie="";&&&&&&&&&&try&&&&&&&&&&{&&&&&&&&&&&&&&URL&url&=&new&URL(htmlurl);&&&&&&&&&&&&&&&&&&&&&&&&&&&&httpConn&=&(HttpURLConnection)&url.openConnection();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&HttpURLConnection.setFollowRedirects(true);&&&&&&&&&&&&&&&&&httpConn.setRequestMethod("POST");&&&&&&&&&&&&&&&&&httpConn.setRequestProperty("User-Agent","Mozilla/5.0&(X11;&Linux&x86_64)&AppleWebKit/537.36&(KHTML,&like&Gecko)&Chrome/33.0.&Safari/537.36");&&&&&&&&&&&&&&&&&httpConn.setRequestProperty("Connection","keep-alive");&&&&&&&&&&&&&&&&&httpConn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml");&&&&&&&&&&&&&&&&&httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");&&&&&&&&&&&&&&&&&httpConn.setRequestProperty("Cache-control","no-cache,&no-store");&&&&&&&&&&&&&&&&&httpConn.setRequestProperty("Host","www.linkedin.com");&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&httpConn.setDoOutput(true);&&&&&&&&&&&&&&&&&httpConn.setDoInput(true);&&&&&&&&&&&&&&&&&httpConn.setUseCaches(false);&&&&&&&&&&&&&&&&&httpConn.setInstanceFollowRedirects(true);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&StringBuffer&str_buf&=&new&StringBuffer(4096);&&&&&&&&&&&&&&&&&OutputStream&os&=&httpConn.getOutputStream();&&&&&&&&&&&&&&&&&str_buf.append("session_key").append("=").append("email").append("&");&&&&&&&&&&&&&&&&&str_buf.append("session_password").append("=").append("gmail").append("&");&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&os.write(str_buf.toString().getBytes());&&&&&&&&&&&&&&&&&os.flush();&&&&&&&&&&&&&&&&&os.close();&&&&&&&&&&&&&&&&&httpConn.setConnectTimeout(20000);&&&&&&&&&&&&&&&&&httpConn.setReadTimeout(20000);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Map<string,list&&map=httpConn.getHeaderFields();&&&&</string,list&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Set&set=map.keySet();&&&&&&&&&&&&&&&&&&&&for&(Iterator&iterator&=&set.iterator();&iterator.hasNext();)&{&&&&&&&&&&&&&&&&&&&&&&&&String&key&=&iterator.next();&&&&&&&&&&&&&&&&&&&&&&&&if(key!=null){&&&&&&&&&&&&&&&&&&&&&&&&&&if&(key.equals("Set-Cookie"))&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.out.println("key="&+&key+",开始获取cookie");&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&List&list&=&map.get(key);&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&for&(String&str&:&list)&{&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&temp=str.split("=")[0];&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&public String login() throws MalformedURLException, InterruptedException{
//Thread.sleep(3000000);
String htmlurl="https://www.linkedin.com/uas/login-submit";
HttpURLConnection httpConn =
String cookie="";
URL url = new URL(htmlurl);
httpConn = (HttpURLConnection) url.openConnection();
HttpURLConnection.setFollowRedirects(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("User-Agent","Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0. Safari/537.36");
httpConn.setRequestProperty("Connection","keep-alive");
httpConn.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml");
httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
httpConn.setRequestProperty("Cache-control","no-cache, no-store");
httpConn.setRequestProperty("Host","www.linkedin.com");
//httpConn.setRequestProperty("Referer","https://www.linkedin.com/uas/login?session_redirect=http://www.linkedin.com/profile/view?id=&authType=name&authToken=fcEe");
//post方法,重定向设置
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setUseCaches(false);
httpConn.setInstanceFollowRedirects(true);
//写入,post方法必须用流写入的方式传输数据
StringBuffer str_buf = new StringBuffer(4096);
OutputStream os = httpConn.getOutputStream();
str_buf.append("session_key").append("=").append("email").append("&");
str_buf.append("session_password").append("=").append("gmail").append("&");
//str_buf.append("session_redirect").append("=").append(redictURL);
os.write(str_buf.toString().getBytes());
os.flush();
os.close();
httpConn.setConnectTimeout(20000);
httpConn.setReadTimeout(20000);
//获取重定向和cookie
//String redictURL= httpConn.getHeaderField( "Location" );
//System.out.println("第一次请求重定向地址 location="+redictURL);
//获取cookie
Map<string,list& map=httpConn.getHeaderFields();
//System.out.println(map.toString());
Set set=map.keySet();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
String key = iterator.next();
if(key!=null){
if (key.equals("Set-Cookie")) {
System.out.println("key=" + key+",开始获取cookie");
List list = map.get(key);
for (String str : list) {
String temp=str.split("=")[0];
//System.out.println(temp);&</string,list[java] &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&if(temp.equals("li_at")){&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&cookie=&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&return&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&httpConn.disconnect();&&&&&}&&catch&(final&MalformedURLException&me)&&{&&&&&System.out.println("url不存在!");&&&&&me.getMessage();&&&&&throw&&&}&&catch&(final&FileNotFoundException&me)&&{&&&&&&System.out.println(htmlurl+"反爬启动");&&&&&return&"0";&&}&&catch&(final&IOException&e)&&{&&&&&&e.printStackTrace();&&&&&&System.out.println("反爬启动:"+htmlurl+"次数:"+countUrl++);&&&&&&httpConn.disconnect();&&&&&&Thread.sleep(20000);&&&&&&return&login();&&}&&&&&&&&&&&&&&return&&&&&
//cookie包含到信息非常多,调试发现登录只需这条信息
if(temp.equals("li_at")){
httpConn.disconnect();
catch (final MalformedURLException me)
System.out.println("url不存在!");
me.getMessage();
catch (final FileNotFoundException me)
System.out.println(htmlurl+"反爬启动");
return "0";
catch (final IOException e)
e.printStackTrace();
System.out.println("反爬启动:"+htmlurl+"次数:"+countUrl++);
httpConn.disconnect();
Thread.sleep(20000);
return login();
//System.out.println(sb);
//return redictURL;
}以上是http处理部分,灵活应用post和get方法,可以获取HTML内容。但是不同网站反爬策略不同。有的封IP,需要登录到有封帐号的,我这个是最简单到断开链接的,直接进程休眠。。。需要换IP,代理,cookie的情况,可以自己分析,基本也就是设置httpConn的一些值。3.数据获取:我一般采用正则匹配,这比较适用于爬取数据不多,网站只返回HTML内容,非常不规范的。。。比如linkedin,所有数据都在一个注释到json里,各种链接和奇怪的符号,用工具很难解析。。。[java] &&&&&&&&&&String&edu="null";&&&&&&&&&&ArrayList&listEdu=new&ArrayList();&&&&&&&&&&String&regex1&=&""fosList":.*?schoolLogo";&&&&&&&&&&Pattern&pa1&=&Pattern.compile(regex1,&Pattern.DOTALL);&&&&&&&&&&Matcher&ma1&=&pa1.matcher(s);&&&&&&&&&&while(ma1.find()){&&&&&&&&&&&&&&EduInfor&ei=new&EduInfor(ui.getCv_id());&&&&&&&&&&&&&&edu=ma1.group();&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&school="null";&&&&&&&&&&&&&&String&regex&=&""schoolName":.*?,";&&&&&&&&&&&&&&Pattern&pa=&Pattern.compile(regex,&Pattern.DOTALL);&&&&&&&&&&&&&&Matcher&ma&=&pa.matcher(edu);&&&&&&&&&&&&&&if(ma.find()){&&&&&&&&&&&&&&&&&&school=ma.group();&&&&&&&&&&&&&&&&&&school=school.replaceAll(""schoolName":",&"");&&&&&&&&&&&&&&&&&&school=school.replaceAll(""",&"");&&&&&&&&&&&&&&&&&&school=school.replaceAll(",",&"");&&&&&&&&&&&&&&&&&&if(!school.equals("")){&&&&&&&&&&&&&&&&&&&&&&ei.setCollege(school);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&degree="null";&&&&&&&&&&&&&&regex&=&""fmt__degree_highlight":.*?,";&&&&&&&&&&&&&&pa=&Pattern.compile(regex,&Pattern.DOTALL);&&&&&&&&&&&&&&ma&=&pa.matcher(edu);&&&&&&&&&&&&&&if(ma.find()){&&&&&&&&&&&&&&&&&&degree=ma.group();&&&&&&&&&&&&&&&&&&degree=degree.replaceAll(""fmt__degree_highlight":",&"");&&&&&&&&&&&&&&&&&&degree=degree.replaceAll(""",&"");&&&&&&&&&&&&&&&&&&degree=degree.replaceAll(",",&"");&&&&&&&&&&&&&&&&&&degree=degree.replaceAll("u0027s",&"");&&&&&&&&&&&&&&&&&&if(!degree.equals("")){&&&&&&&&&&&&&&&&&&&&&&ei.setDegree_name(degree);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&major="null";&&&&&&&&&&&&&&regex&=&""fmt__fos_highlight":.*?,";&&&&&&&&&&&&&&pa=&Pattern.compile(regex,&Pattern.DOTALL);&&&&&&&&&&&&&&ma&=&pa.matcher(edu);&&&&&&&&&&&&&&if(ma.find()){&&&&&&&&&&&&&&&&&&major=ma.group();&&&&&&&&&&&&&&&&&&major=major.replaceAll(""fmt__fos_highlight":",&"");&&&&&&&&&&&&&&&&&&major=major.replaceAll(""",&"");&&&&&&&&&&&&&&&&&&major=major.replaceAll(",",&"");&&&&&&&&&&&&&&&&&&if(!major.equals("")){&&&&&&&&&&&&&&&&&&&&&&ei.setMajor(major);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&academic="null";&&&&&&&&&&&&&&regex&=&""grade":.*?,";&&&&&&&&&&&&&&pa=&Pattern.compile(regex,&Pattern.DOTALL);&&&&&&&&&&&&&&ma&=&pa.matcher(edu);&&&&&&&&&&&&&&if(ma.find()){&&&&&&&&&&&&&&&&&&academic=ma.group();&&&&&&&&&&&&&&&&&&academic=academic.replaceAll(""grade":",&"");&&&&&&&&&&&&&&&&&&academic=academic.replaceAll(""",&"");&&&&&&&&&&&&&&&&&&academic=academic.replaceAll(",",&"");&&&&&&&&&&&&&&&&&&if(!academic.equals("")){&&&&&&&&&&&&&&&&&&&&&&ei.setAcademic_name(academic);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&s_time="null";&&&&&&&&&&&&&&regex&=&""startdate_my":.*?,";&&&&&&&&&&&&&&pa=&Pattern.compile(regex,&Pattern.DOTALL);&&&&&&&&&&&&&&ma&=&pa.matcher(edu);&&&&&&&&&&&&&&if(ma.find()){&&&&&&&&&&&&&&&&&&s_time=ma.group();&&&&&&&&&&&&&&&&&&s_time=s_time.replaceAll(""startdate_my":",&"");&&&&&&&&&&&&&&&&&&s_time=s_time.replaceAll(""",&"");&&&&&&&&&&&&&&&&&&s_time=s_time.replaceAll(",",&"");&&&&&&&&&&&&&&&&&&s_time=s_time.replaceAll("&",&"");&&&&&&&&&&&&&&&&&&if(!s_time.equals("")){&&&&&&&&&&&&&&&&&&&&&&ei.setStart_time(s_time);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&&&&&&&&&String&e_time="null";&&&&&&&&&&&&&&regex&=&""enddate_my":.*?,";&&&&&&&&&&&&&&pa=&Pattern.compile(regex,&Pattern.DOTALL);&&&&&&&&&&&&&&ma&=&pa.matcher(edu);&&&&&&&&&&&&&&if(ma.find()){&&&&&&&&&&&&&&&&&&e_time=ma.group();&&&&&&&&&&&&&&&&&&e_time=e_time.replaceAll(""enddate_my":",&"");&&&&&&&&&&&&&&&&&&e_time=e_time.replaceAll(""",&"");&&&&&&&&&&&&&&&&&&e_time=e_time.replaceAll(",",&"");&&&&&&&&&&&&&&&&&&e_time=e_time.replaceAll("&",&"");&&&&&&&&&&&&&&&&&&if(!e_time.equals("")){&&&&&&&&&&&&&&&&&&&&&&ei.setEnd_time(e_time);&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&}else{&&&&&&&&&&&&&&&&&&ei.setEnd_time("目前");&&&&&&&&&&&&&&}&&&&&&&&&&&&&&listEdu.add(ei);&&&&&&&&&&&&&&&&&&&&&&&&}&&//教育信息"fosList":.*?schoolLogo
String edu="null";
ArrayList listEdu=new ArrayList();
String regex1 = ""fosList":.*?schoolLogo";
Pattern pa1 = Pattern.compile(regex1, Pattern.DOTALL);
Matcher ma1 = pa1.matcher(s);
while(ma1.find()){
EduInfor ei=new EduInfor(ui.getCv_id());
edu=ma1.group();
String school="null";
String regex = ""schoolName":.*?,";
Pattern pa= Pattern.compile(regex, Pattern.DOTALL);
Matcher ma = pa.matcher(edu);
if(ma.find()
刚表态过的朋友 ()
dataguru.cn All Right Reserved.DC学院数据分析学习笔记(二):爬虫需要的HTML
DC学院数据分析学习笔记(二):爬虫需要的HTML
简单记一下爬虫需要的HTML
关于html,之前也稍微了解过一些,又碰到了,那么就系统的学习一下
超文本标记语言(HyperText Markup Language,简称:HTML)是一种用于创建网页的标准标记语言。
什么是 HTML?
HTML 是用来描述网页的一种语言。
HTML 指的是超文本标记语言 (Hyper Text Markup Language)
HTML 不是一种编程语言,而是一种标记语言 (markup language)
标记语言是一套标记标签 (markup tag)
HTML 使用标记标签来描述网页
HTML 文档包含了HTML 标签及文本内容
HTML文档也叫做 web 页面
HTML代码的一般形式
一个基本的网页代码框架:
&!DOCTYPE html&
&title&文档标题&/title&
文档内容......
标签基本格式:&标签名 属性名1=“属性值” 属性名2=“属性值” ……&文件内容标签名&
&! DOCTYPE html&:用于代码开头指定html版本等信息
&html&&/html&:告知浏览器这是一个 HTML 文档,是 HTML 文档中最外层的元素
&head&&/head&:所有头部元素的容器,必须包含文档的标题(title),可以包含脚本、样式、meta 信息以及其他
&title&&/title&:定义文档的标题,定义浏览器工具栏中的标题,显示在搜索引擎结果中的页面标题
&body&&/body&:定义文档的主体,包含文档的所有内容(比如文本、超链接、图像、表格和列表等)
&h1&&/h1&:定义 HTML 标题,从&h1&到&h6&标题的重要程度逐渐降低
&p&&/p&:定义段落,浏览器会自动在其前后创建一些空白
&br&:一个简单的换行符,是一个空标签,意味着它没有结束标签。
&div&&/div&:定义 HTML 文档中的一个分隔区块或者一个区域部分。经常与 CSS 一起使用,用来布局网页。
用菜鸟教程的HTML网页结构举个例子:
HTML链接语法
&a href="url"&Link text&/a&:href 属性规定链接的目标。
&a href="form.html"&Fill Our Form&/a&:指向同一服务器同一目录下的form.html
&a href="../parent.html"&Parent&/a&:指向同一服务器父目录下的parent.html
&a href="stuff/cat.html"&Catalog&/a& :指向同一服务器子目录stuff下的cat.html
&a href="https://baidu.com" target="_blank"&baidu&/a&:指向外部网站 。其中,使用了 Target 属性,可以定义被链接的文档在何处显示,这里的会在新窗口打开网页
特别的,在HTML链接中有个id属性
id属性可用于创建在一个HTML文档书签标记。
提示: 书签是不以任何特殊的方式显示,在HTML文档中是不显示的,所以对于读者来说是隐藏的
引用中的例子:
&img src="url" alt="some_text"&:src 指 "source",即图像的 URL 地址。alt属性是在图片无法显示时,替换上去的文本。
和超链接结合起来,可以为插入的图片添加超链接:
&a href="test.html"&&img src="test.jpg" /&&/a&
还可以设置图像的高度于宽度:
&img src="pulpit.jpg" alt="Pulpit rock" width="304" height="228"&:最好设置一下,因为没有指定图片的大小,加载页面时有可能破坏HTML的整体布局
无序列表(unordered list,ul)
&li&Coffee&/li&
&li&Milk&/li&
有序列表(ordered list,ol)
&li&Coffee&/li&
&li&Milk&/li&
&table&&/table& :表格的开始和结束
&tr&&/tr& :创建表格的一行
&td&&/td& :创建表格中普通单元格
&th&&/th&:创建表格中标题栏单元格
表单元素是允许用户在表单中输入内容,比如:文本域(textarea)、下拉列表、单选框(radio-buttons)、复选框(checkboxes)等等。
引用一个DC学院课堂中的例子:
OK !HTML内容还是很多的,说是系统学习一下,其实只学了一下爬虫会可能用到的。
用云栖社区APP,舒服~
【云栖快讯】青年们,一起向代码致敬,来寻找第83行吧,云栖社区邀请大神彭蕾、多隆、毕玄、福贝、点评Review你的代码,参与互动者将选取50位精彩回复赠送“向代码致敬”定制T恤1件,最终成为“多隆奖”的小伙伴还将获得由阿里巴巴提供的“多隆奖”荣誉证书和奖杯。&&
阿里云机器学习是基于阿里云分布式计算引擎的一款机器学习算法平台。用户通过拖拉拽的方式可视化的...
Node.js 性能平台(Node.js Performance Platform)是面向中...
深度挖掘企业与企业、企业与人物的关系,通过多位交叉分析及智能算法,构建基于企业全息画像和企业...
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效...

我要回帖

更多关于 爬虫 标签属性 的文章

 

随机推荐