您的位置: 首页 - 站长

seo整站优化新站快速排名wordpress重置密码链接无效

当前位置: 首页 > news >正文

seo整站优化新站快速排名,wordpress重置密码链接无效,巴中哪里可以做公司网站,商务网站建设过程简单介绍一下爬虫原理。并给出 51job网站完整的爬虫方案。 爬虫基础知识 数据来源 网络爬虫的数据一般都来自服务器的响应结果#xff0c;通常有html和json数据等#xff0c;这两种数据也是网络爬虫的主要数据来源。 其中html数据是网页的源代码#xff0c;通过浏览器-查… 简单介绍一下爬虫原理。并给出 51job网站完整的爬虫方案。 爬虫基础知识 数据来源 网络爬虫的数据一般都来自服务器的响应结果通常有html和json数据等这两种数据也是网络爬虫的主要数据来源。 其中html数据是网页的源代码通过浏览器-查看源代码可以直接查看例如 json是一种数据存储格式往往包含了最原始的数据内容一般不直接显示在网页中这里可以通过Chrome浏览器开发者工具中的Network选项捕获到服务器返回的json数据例如 数据请求 数据请求的方式一般有两种GET方法和POST方法。也可以通过Chrome浏览器来捕获访问一个浏览器时的所有请求。这里以简书主页为例打开Chrome浏览器-开发者工具F12切换到Network选项在地址栏输入http://www.jianshu.com/ 选择XHR类型可以看到一条请求的内容打开Headers在General中可以看到请求方式为GET方式 其中的Request Headers便是访问这个网页时的请求数据如下图。 这个Headers可以用Python中的字典来表示包含了用户请求的一些信息例如编码、语言、用户登陆信息、浏览器信息等。 下面还有一个Query String Parameters这里面包含了用户请求的一些参数也是请求数据的一部分。 利用requests库请求数据 利用Python构建数据请求的方式有很多在python3中主要有urllib和requests两个类库可以实现该功能。urllib是官方标准库其官方文档传送门。这里主要介绍第三方库requests它是基于urllib编写的比urllib用起来更加便捷可以节约时间。 requests安装方法 \( pip install requests利用requests构建数据请求主要方式 import requests req request.get(url)或者 import requests req requests.post(url)其中get()与post()中都可以添加headers、params等参数以字典的形式传递即可。一般来说简单的网页通过传入url数据即可成功请求数据。不过一些网站采用了反爬虫机制需要传入headers及params等参数以模拟浏览器访问、用户登陆等行为才可以正常请求数据。 利用webdriver请求数据 webdriver是一个用来进行复杂重复的web自动化测试的工具能够使用chrome、firefox、IE浏览器进行web测试可以模拟用户点击链接填写表单点击按钮等。因此相对于requests库来说webdriver在模拟浏览器鼠标点击滑动等事件上有着天然的优势并且真实模拟了浏览器的操作不易被反爬虫机制发现因此是一个很好用的爬虫工具。当然其缺点在于速度较慢效率不高。 webdriver安装 \) pip install selnium除了安装selnium库webdriver的运行还需要进行浏览器驱动的配置。Chrome、火狐和IE浏览器都有其配置方式具体方法查看 链接。 这里以IE浏览器为例做一个简单的示范 from selenium import webdriver import os iedriver IEDriverServer.exe os.environ[webdriver.ie.driver] iedriver driver webdriver.Ie(iedriver)如此IE浏览器配置完毕其中IEDriverServer.exe是IE浏览器驱动的存储路径。 于是访问简书网主页数据只需要一步 driver.get(http://www.jianshu.com/)数据解析 使用requests请求下来的数据可以利用.text()方法或者.content()方法访问对于文本请求二者并无太大差别主要在于编码问题。具体用法可以参考官方文档这里不再赘述。使用webdriver请求下来的数据可以用.page_source属性获取。请求下来的数据一般包含了大量的网页源代码如何将其解析以提取出想要的内容 html类型数据解析 html语言即超文本标记语言它是由一个个html标签构成的是结构化的语言因此很容易从中匹配提取信息。这种类型的数据解析的方法有很多比如利用正则表达式按照html标签的结构进行字符串匹配或则利用lxml库中的xpath方法使用xpath路径定位到每一个节点、也有类似jQuery的PyQuery方法。这里主要介绍BeautifulSoup方法。 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间。该介绍来源于其官方中文文档传送门。利用BeautifulSoup能够将html字符串转化为树状结构并非常快速地定位到每一个标签。 目前版本是BeautifulSoup4pip安装方法 \( pip install BeautifulSoup4或者下载bs4的源码然后解压并运行 \) python setup.py install 利用BeautifulSoup解析html数据的关键步骤为 from bs4 import BeautifulSoup soup BeautifulSoup(req.contents, html.parser)如果采用webdriver请求数据那么 from bs4 import BeautifulSoup soup BeautifulSoup(driver.page_source, html.parser)如此便将html数据转换成BeautifulSoup中的树状结构。然后利用BeautifulSoup中的find()、find_all()等方法即可定位到每一个节点。详情请参阅 官方文档。 json类型数据解析 json类型的数据已经是高度结构化的数据跟Python中字典的表示形式一样因此在解析上十分方便。可以通过 import json data json.loads(req.text)直接读取json数据且能够返回字典类型。 大数据职位数据爬虫实战 这里以51job网站为例构建大数据相关职位的数据爬虫。其中搜索关键词为 数据科学家 数据分析师 数据架构师 数据工程师 统计学家 数据库管理员 业务数据分析师 数据产品经理网页分析 打开51job首页http://www.51job.com/ 在搜索框中输入“数据科学家”将搜索框中的地区点开去掉当前勾选的城市即默认在全国范围搜索。点击“搜索”按钮得到搜索结果。这时将网址栏URL复制出来 http://search.51job.com/list/000000,000000,0000,00,9,99, %25E6%2595%25B0%25E6%258D%25AE%25E7%25A7%2591%25E5%25AD%25A6%25E5%25AE%25B6, 2,1.html?langcstypepostchannel0000workyear99cotype99degreefrom99 jobterm99companysize99providesalary99lonlat0%2C0radius-1ord_field0 confirmdate9fromTypedibiaoid0addresslinespecialarea00fromwelfare结果不止一页点击第二页同样将URL复制出来 http://search.51job.com/list/000000,000000,0000,00,9,99, %25E6%2595%25B0%25E6%258D%25AE%25E7%25A7%2591%25E5%25AD%25A6%25E5%25AE%25B6, 2,2.html?langcstype1postchannel0000workyear99cotype99degreefrom99 jobterm99companysize99lonlat0%2C0radius-1ord_field0 confirmdate9fromTypedibiaoid0addresslinespecialarea00fromwelfare很容易发现这两段url唯一的不同在于.html前面的数字1和2因此它代表了页码。其中 %25E6%2595%25B0%25E6%258D%25AE%25E7%25A7%2591%25E5%25AD%25A6%25E5%25AE%25B6是一种URL编码翻译成中文就是“数据科学家”转换方式可以使用urllib库中的quote()方法 import urllib.quote keyword 数据科学家 url quote(keyword)可以通过第一次的搜索结果获取页码数 def GetPages(keyword):keyword quote(keyword, safe/:?)url http://search.51job.com/jobsearch/search_result.php?fromJs1jobarea000000%2C00district000000funtype0000industrytype00issuedate9providesalary99keywordkeyword \keywordtype2curr_page1langcstype1postchannel0000workyear99cotype99degreefrom99jobterm99companysize99lonlat0%2C0radius-1ord_field0list_type0fromType14dibiaoid0confirmdate9html requests.get(url)soup BeautifulSoup(html.content, html.parser)span soup.find(div, class_p_in).find(span, class_td)page_num span.get_text().replace(共, ).replace(页到第, )return page_num由此便可实现针对特定关键词的所有搜索结果的页面的遍历。 URL列表构建 打开搜索结果页面会发现点击职位名称可以链接到每个职位的详情页面也正是所需要的数据源。因此只需要获取所有的搜索结果中的职位名称的超链接地址便可以遍历所有职位的详细数据 def GetUrls(keyword, page_num):keyword quote(keyword, safe/:?)urls []p page_num1for i in range(1, p):url http://search.51job.com/jobsearch/search_result.php?fromJs1jobarea000000%2C00district000000funtype0000industrytype00issuedate9providesalary99keywordkeyword \keywordtype2curr_page \str(i) \langcstype1postchannel0000workyear99cotype99degreefrom99jobterm99companysize99lonlat0%2C0radius-1ord_field0list_type0dibiaoid0confirmdate9html requests.get(url)soup BeautifulSoup(html.content, html.parser)ps soup.find_all(p, class_t1)for p in ps:a p.find(a)urls.append(str(a[href]))s random.randint(5, 30)print(str(i)page done,str(s)s later)time.sleep(s)return urls构造数据请求 在获取了所有的职位数据的url之后使用requests访问这些url发现并不能顺利获取数据。因此可以考虑在请求中加入headers数据其中包含cookie和User_Agent User_Agent Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 cookie guid14842945278988500031; slifeindexguide%3D1 headers {User-Agent: User_Agent, cookie: cookie}这样可以成功请求每个职位的详情页面数据 数据解析 数据解析首先是明确数据需求这里将数据尽可能多的抓取下来。 以职位要求一栏为例通过访问多个页面对比发现这一栏可能显示的要求个数不一样 这里包括了经验、学历、招聘人数和发布时间 而这里则没有对于经验的要求。 利用浏览器开发者选项功能查看这一栏的源码 这里职位的要求都放在一个classsp4的span中通过查找功能可以发现没有其他的classsp4的标签所以利用find_all()方法可以轻松定位到这些职位要求数据。 通过比较可以发现这最多的要求个数为4所以在个数不确定的情况下可以先新建一个包含四个空字符串元素的新数组将所有的要求个数填入该数组这样可以保证不同网页的数据都能获取完整。 spans soup.find_all(span, class_sp4) num len(spans) nav [, , , ] for i in range(0, num-1):nav[i] spans[i].get_text().strip()完整代码如下

-- coding: utf-8 --

from urllib.parse import quote import requests from bs4 import BeautifulSoup import time import randomdef GetPages(keyword):keyword quote(keyword, safe/:?)url http://search.51job.com/jobsearch/search_result.php?fromJs1jobarea000000%2C00district000000funtype0000industrytype00issuedate9providesalary99keywordkeyword \keywordtype2curr_page1langcstype1postchannel0000workyear99cotype99degreefrom99jobterm99companysize99lonlat0%2C0radius-1ord_field0list_type0fromType14dibiaoid0confirmdate9html requests.get(url)soup BeautifulSoup(html.content, html.parser)span soup.find(div, class_p_in).find(span, class_td)page_num span.get_text().replace(共, ).replace(页到第, )return page_numdef GetUrls(keyword, page_num):keyword quote(keyword, safe/:?)urls []p page_num1for i in range(1, p):url http://search.51job.com/jobsearch/search_result.php?fromJs1jobarea000000%2C00district000000funtype0000industrytype00issuedate9providesalary99keywordkeyword \keywordtype2curr_page \str(i) \langcstype1postchannel0000workyear99cotype99degreefrom99jobterm99companysize99lonlat0%2C0radius-1ord_field0list_type0dibiaoid0confirmdate9html requests.get(url)soup BeautifulSoup(html.content, html.parser)ps soup.find_all(p, class_t1)for p in ps:a p.find(a)urls.append(str(a[href]))s random.randint(5, 30)print(str(i)page done,str(s)s later)time.sleep(s)return urlsdef GetContent(url, headers):html requests.get(url, headersheaders)soup BeautifulSoup(html.content, html.parser)PositionTitle str(soup.find(h1)[title])Location soup.find(span, class_lname).stringSalary soup.find(strong).stringCompanyName soup.find(p, class_cname).get_text().strip()CompanyType soup.find(p, class_msg ltype).get_text().strip().replace( , ).replace( , ).replace( , ).replace( , )spans soup.find_all(span, class_sp4)num len(spans)nav [, , , ]for i in range(0, num-1):nav[i] spans[i].get_text().strip()Exp nav[0]Degree nav[1]RecruitNum nav[2]PostTime nav[3]Welfare soup.find(p, class_t2)if str(type(Welfare)) class NoneType:Welfare else:Welfare Welfare.get_text().strip().replace(\n, |)PositionInfo soup.find(div, class_bmsg job_msg inbox).get_text().strip().replace(\n, ).replace(分享, ).replace(举报, ).replace( , ).replace( , ).replace( , ).replace( , ).replace(\r, )PositionType soup.find(span, class_el)if str(type(PositionType)) class NoneType:PositionType else:PositionType PositionType.get_text().strip().replace(\n, )Contact soup.find(div, class_bmsg inbox)if str(type(Contact)) class NoneType:Contact else:Contact Contact.get_text().strip().replace( , ).replace( , ).replace(地图, ).replace(\n, )ConpanyInfo soup.find(div, class_tmsg inbox)if str(type(ConpanyInfo)) class NoneType:ConpanyInfo else:ConpanyInfo ConpanyInfo.get_text().strip().replace(\n, ).replace( , ).replace( , )try:record PositionTitle\tLocation\tSalary\tCompanyName\tCompanyType\tExp\tDegree\t \RecruitNum\tPostTime\tWelfare\tPositionInfo \tstr(PositionType)\tstr(Contact)\tstr(ConpanyInfo)except Exception as e:record else:passfinally:passreturn recorddef main():with open(keywords.txt, r, encodingutf-8) as f:keywords f.readlines()for keyword in keywords[1:]:keyword keyword.strip()page_num int(GetPages(keyword))urls GetUrls(keyword, page_num)with open(keywordurls.txt, w, encodingutf-8) as f:for url in urls:f.write(url\n)User_Agent Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36cookie guid14842945278988500031; slifeindexguide%3D1headers {User-Agent: User_Agent, cookie: cookie}with open(keywordurls.txt, r, encodingutf-8) as f:urls f.readlines()records []i 0for url in urls:url url.strip()if url ! :records.append(GetContent(url, headers))i 1s random.randint(5, 30)print(str(i)page done,str(s)s later)time.sleep(s)with open(keyword.txt, w, encodingutf-8) as f:for re in records:f.write(re\n)print(keyword Done—————————)if name main:main()