jquery 抓取微博

by Codemzy

由Codemzy

使用jQuery和RegexJavaScript进行客户端网络抓取 (Client-side web scraping with JavaScript using jQuery and Regex)

When I was building my first open-source project, codeBadges, I thought it would be easy to get user profile data from all the main code learning websites.

当我构建第一个开源项目codeBadges时,我认为从所有主要的代码学习网站上获取用户资料数据将很容易。

I was familiar with API calls and get requests. I thought I could just use jQuery to fetch the data from the various API’s and use it.

我熟悉API调用和获取请求。 我以为我可以使用jQuery从各种API提取数据并使用它。

var name = 'codemzy';                $.get('https://api.github.com/users/' + name, function(response) {                      var followers = response.followers;});

Well, that was easy. But it turns out that not every website has a public API that you can just grab the data you want from.

好吧,那很容易。 但事实证明,并非每个网站都有一个公共API,您可以从中获取所需的数据。

But just because there is no public API doesn’t mean you need to give up! You can use web scraping to grab the data, with only a little extra work.

但是,仅仅因为没有公共API并不意味着您需要放弃! 您可以使用Web抓取来获取数据,而只需做一些额外的工作

Let’s see how we can use client-side web scraping with JavaScript.

让我们看看如何在JavaScript中使用客户端Web抓取。

For an example, I will grab my user information from my public freeCodeCamp profile. But you can use these steps on any public HTML page.

例如,我将从公共的freeCodeCamp配置文件中获取用户信息。 但是您可以在任何公共HTML页面上使用这些步骤。

The first step in scraping the data is to grab the full page html using a jQuery .get request.

抓取数据的第一步是使用jQuery .get请求获取整页html。

var name = "codemzy";$.get('https://www.freecodecamp.com/' + name, function(response) {  console.log(response);});

Awesome, the whole page source code just logged to the console.

太棒了,整个页面的源代码都刚刚登录到控制台。

Note: If you get an error at this stage along the lines of No ‘Access-Control-Allow-Origin’ header is present on the requested resource don’t fret. Scroll down to the Don’t Let CORS Stop You section of this post.

注意:如果您在此阶段遇到错误, No 'Access-Control-Allow-Origin' header is present on the requested resource 不要担心。 向下滚动到这篇文章的“ 不要让CORS阻止您”部分。

That was easy. Using JavaScript and jQuery, the above code requests a page from www.freecodecamp.org, like a browser would. And freeCodeCamp responds with the page. Instead of a browser running the code to display the page, we get the HTML code.

那很简单。 上面的代码使用JavaScript和jQuery,从www.freecodecamp.org请求页面,就像浏览器一样。 而freeCodeCamp则以页面响应。 我们得到的是HTML代码,而不是浏览器运行代码显示页面。

And that’s what web scraping is, extracting data from websites.

这就是从网站提取数据的网络抓取。

Ok, the response is not exactly as neat as the data we get back from an API.

好的,响应与我们从API返回的数据并不完全一样。

But… we have the data, in there somewhere.

但是……我们在某处有数据。

Once we have the source code the information we need is in there, we just have to grab the data we need!

一旦我们有了源代码,就可以在其中找到所需的信息,我们只需要获取所需的数据即可!

We can search through the response to find the elements we need.

我们可以搜索响应以找到我们需要的元素。

Let’s say we want to know how many challenges the user has completed, from the user profile response we got back.

假设我们想从返回的用户个人资料响应中知道用户已经完成了多少个挑战。

At the time of writing, a camper’s completed challenges completed are organized in tables on the user profile. So to get the total number of challenges completed, we can count the number of rows.

在撰写本文时,露营者已完成的挑战已在用户个人资料的表格中组织。 因此,要完成挑战的总数,我们可以计算行数。

One way is to wrap the whole response in a jQuery object, so that we can use jQuery methods like .find() to get the data.

一种方法是将整个响应包装在jQuery对象中,以便我们可以使用.find()类的jQuery方法来获取数据。

// number of challenges completedvar challenges = $(response).find('tbody tr').length;

This works fine — we get the right result. But its is not a good way to get the result we are after. Turning the response into a jQuery object actually loads the whole page, including all the external scripts, fonts and stylesheets from that page…Uh oh!

效果很好-我们得到了正确的结果。 但这不是获得我们追求的结果的好方法 。 将响应转换为jQuery对象实际上会加载整个页面,包括该页面上的所有外部脚本,字体和样式表……哦!

We need a few bits of data. We really don’t need the page the load, and certainly not all the external resources that come with it.

我们需要一些数据。 我们确实不需要页面加载,当然也不需要页面附带的所有外部资源。

We could strip out the script tags and then run the rest of the response through jQuery. To do this, we could use Regex to look for script patterns in the text and remove them.

我们可以剥离脚本标签,然后通过jQuery运行其余的响应。 为此,我们可以使用Regex在文本中查找脚本模式并将其删除。

Or better still, why not use Regex to find what we are looking for in the first place?

还是更好,为什么不使用Regex首先找到我们想要的东西?

// number of challenges completedvar challenges = response.replace(/<thead>[\s|\S]*?<\/thead>/g).match(/<tr>/g).length;

And it works! By using the Regex code above, we strip out the table head rows (that did not contain any challenges), and then match all table rows to count the number of challenges completed.

而且有效! 通过使用上面的Regex代码,我们去除了表头行(不包含任何挑战),然后匹配所有表行以计算完成的挑战数。

It’s even easier if the data you want is just there in the response in plain text. At the time of writing the user points were in the html like <h1 class=”flat-top text-primary”>[ 1498 ]</h1> just waiting to be scraped.

如果所需的数据以纯文本形式出现在响应中,则更加容易。 在编写本文时,用户点在html中,例如<h1 class=”flat-top text-primary”>[ 1498 ] </ h1>只是在等待被抓取。

var points = response.match(/<h1 class="flat-top text-primary">\[ ([\d]*?) \]<\/h1>/)[1];

In the above Regex pattern we match the h1 element we are looking for including the [ ] that surrounds the points, and group any number inside with ([\d]*?). We get an array back, the first [0] element is the entire match and the second [1] is our group match (our points).

在上面的Regex模式中,我们匹配要寻找的h1元素,包括包围点的[ ] ,并用([\d]*?).分组内部的任何数字([\d]*?). 我们得到一个数组,第一个[0]元素是整个比赛,第二个[1]是我们的小组比赛(我们的得分)。

Regex is useful for matching all sorts of patterns in strings, and it is great for searching through our response to get the data we need.

正则表达式对于匹配字符串中的各种模式很有用,对于搜索我们的响应以获取所需的数据非常有用。

You can use the same 3 step process to scrape profile data from a variety of websites:

您可以使用相同的3个步骤来从各种网站抓取个人资料数据:

  1. Use client-side JavaScript使用客户端JavaScript
  2. Use jQuery to scrape the data使用jQuery抓取数据
  3. Use Regex to filter the data for the relevant information使用Regex过滤数据以获取相关信息

Until I hit a problem, CORS.

在我遇到问题之前,CORS。

不要让CORS阻止您! (Don’t Let CORS Stop You!)

CORS or Cross-Origin Resource Sharing, can be a real problem with client-side web scraping.

CORS或跨域资源共享可能是客户端Web抓取的真正问题。

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. And because we are using client-side Javascript on the front end for web scraping, CORS errors can occur.

出于安全原因,浏览器限制了从脚本内部发起的跨域HTTP请求。 并且因为我们在前端使用客户端Javascript进行Web抓取,所以可能会发生CORS错误。

Here’s an example trying to scrape profile data from CodeWars…

这是一个示例,尝试从CodeWars抓取配置文件数据…

var name = "codemzy";$.get('https://www.codewars.com/users/' + name, function(response) {  console.log(response);});

At the time of writing, running the above code gives you a CORS related error.

在撰写本文时,运行上述代码会给您提供CORS相关错误。

If there is noAccess-Control-Allow-Origin header from the place you’re scraping, you can run into problems.

如果您要剪贴的地方没有Access-Control-Allow-Origin标头,则可能会遇到问题。

The bad news is, you need to run these sorts of requests server-side to get around this issue.

坏消息是,您需要在服务器端运行这些类型的请求才能解决此问题。

Whaaaaaaaat, this is supposed to be client-side web scraping?!

哇,这应该是客户端网络抓取吗?

The good news is, thanks to lots of other wonderful developers that have run into the same issues, you don’t have to touch the back end yourself.

好消息是,由于其他许多出色的开发人员都遇到了同样的问题,因此您不必自己碰后端。

Staying firmly within our front end script, we can use cross-domain tools such as Any Origin, Whatever Origin, All Origins, crossorigin and probably a lot more. I have found that you often need to test a few of these to find the one that will work on the site you are trying to scrape.

牢牢地掌握在前端脚本中,我们可以使用跨域工具,例如Any Origin , Whatever Origin , All Origins , crossorigin等等。 我发现您经常需要测试其中一些才能找到要在您要抓取的网站上运行的软件。

Back to our CodeWars example, we can send our request via a cross-domain tool to bypass the CORS issue.

回到我们的CodeWars示例,我们可以通过跨域工具发送请求以绕过CORS问题。

var name = "codemzy";var url = "http://anyorigin.com/go?url=" + encodeURIComponent("https://www.codewars.com/users/") + name + "&callback=?";$.get(url, function(response) {  console.log(response);});

And just like magic, we have our response.

就像魔术一样,我们有回应。

翻译自: https://www.freecodecamp.org/news/client-side-web-scraping-with-javascript-using-jquery-and-regex-5b57a271cb86/

jquery 抓取微博

jquery 抓取微博_使用jQuery和RegexJavaScript进行客户端网络抓取相关推荐

  1. python爬取微博内容_Python 爬虫如何机器登录新浪微博并抓取内容?

    最近为了做事件分析写了一些微博的爬虫,两个大V总共爬了超70W的微博数据. 官方提供的api有爬取数量上限2000,想爬取的数据大了就不够用了... 果断撸起袖子自己动手!先简单说一下我的思路: 一. ...

  2. jquery滚动条平滑滚动_使用jQuery平滑垂直或水平页面滚动

    jquery滚动条平滑滚动 View demo 查看演示Download Source 下载源 In this tutorial we will create a simple smooth scro ...

  3. jquery 获取元素图片_【jQuery】获取图片真实宽高

    jQuery有没有获取图片实际尺寸的方法? 就是图片文件的实际尺寸,而不是添加了css样式之后的尺寸. 肥肠真实,如假包换! //code from http://caibaojian.com/jqu ...

  4. jquery给日期赋值_关于jQuery赋值

    你的位置: 问答吧 -> XML/HTML/CSS/JS -> 问题详情 关于jQuery赋值 我先取了文本长度 var val = $("#dd").text(); ...

  5. jQuery 一次定时器_用 jQuery 手写一个小游戏

    作者:王圣松 转发链接:https://juejin.im/post/6844903687307919373 前言 今天给大家带来一个小游戏. 要求:熟悉 JavaScript 继承的概念. 游戏预览 ...

  6. jquery 树形框 横_利用jQuery设计横/纵向菜单

    在网页中,菜单扮演着"指路者"的角色.如何设计一个人性化的菜单呢,下面小编带着大家一起做. 效果图: 设计历程: 1.首先利用html中的 和 标签进行嵌套,搭起一个整体的框架.如 ...

  7. jquery检测input变化_检测jQuery中的输入变化?

    翻过高山走不出你 更新以作澄清和举例例子:http://jsfiddle.net/pxfunc/5kpeJ/方法1.input事件在现代浏览器中,使用input事件.当用户在文本字段中键入.粘贴.撤消 ...

  8. jquery文档就绪_在jQuery中,下列关于文档就绪函效的写法错误的是( )

    [单选题]下图是哪种辉石.() [单选题]人体的肌肉主要是由蛋白质构成的,但骨骼肌.心肌.平滑肌的功能各不相同,这是因为 ( ) [判断题]宝石级方柱石首次发现于缅甸. [判断题]市场上最主要的辉石品 ...

  9. jquery.form 异步校验_利用jQuery.validate异步验证用户名是否存在

    HTML头部引用: HTML内容(部分): 用户名: JS代码部分: $(function() { //表单验证JS $("#form1").validate({ //出错时添加的 ...

  10. python登录网站后爬取数据_如何用 Python 登录主流网站,爬取一些需要的数据

    最近收集了一些网站的登陆方式和爬虫程序,有的通过 selenium 登录,有的则通过抓包直接模拟登录.作者希望该项目能帮助初学者学习各大网站的模拟登陆方式,并爬取一些需要的数据. 作者表示模拟登陆基本 ...

最新文章

  1. 我是怎么使用最短路径算法解决动态联动问题的
  2. vue 组件属性监听_Vue.js 监听属性
  3. C# WinForm 添加Windows Media Player 控件调试出现未能加载文件或程序集Interop.WMPLib,该怎么解决...
  4. assign, retain, copy, weak, strong
  5. mysql not exists无效_分析MySQL中哪些情况下数据库索引会失效
  6. MySQL 导出命令
  7. 互联网日报 | 3月12日 星期五 | 京东全年净增1.1亿活跃用户;百度计划3月23日登陆港交所;中国联通首次公布5G用户数...
  8. Bootstrap 3: 图标转换事件 Change icons when toggle
  9. c 语言 小波变换,小波变换C语言
  10. python做excel自动化-Python控制Excel实现自动化办公
  11. 【刷题】BZOJ 5154 [Tjoi2014]匹配
  12. BP神经网络matlab代码
  13. 如何批量将图片转换为 Excel 文档
  14. MATLAB---三维绘制函数实例介绍
  15. 如何一步步打造完美的成绩查询系统平台?
  16. FPGA学习思维导图
  17. 有哪些适合大学生投稿的普刊(社科类)?
  18. 深度剖析家用洗地机的方案设计
  19. jsp+ssh2+mysql实现的CRM客户关系管理系统
  20. 计算机自带远程桌面设置与连接方式步骤

热门文章

  1. ue4 FString 中文乱码问题
  2. [连载2]互联网究竟是什么怪物…他们不愿公开真正秘密...
  3. python 表情包爬虫
  4. Dell r230安装centOS6.5图解
  5. java经纬度曲线简化_JAVA 后台计算 经纬度 最短距离
  6. VOA 2009年11月23日 星期一 这里是美国——感恩节对美国人来说意味着什么
  7. 不拆微动,一招解决鼠标双击问题
  8. idea 替换 choose initializr service URL.
  9. reduceByKey中的加号是什么意思
  10. 1194_SICP学习笔记_霍夫曼编码树