java WebMagic 爬虫

1 篇文章 0 订阅
1 篇文章 0 订阅


由于公司团建,过了两天才来写博客,记录下自己采坑的过程

2020-08-19 下午

老大说我们数据库的数据量现在太小,想往里面灌些数据进去,我跟他说直接些存储过程,他说数据要真实性的,让我写一个爬虫往里面灌输数据
然后给了我两个网站,一个是博客园的新闻 "https://news.cnblogs.com/" 一个是InfoQ的 "https://www.infoq.cn/"
看了下博客园的比较简单,然后就开始写
问题
1.当抓取数据到第10页时,数据一直抓不到,然后看日志发现,从第10以后的每个页都需登录之后才能得到想要的数据,然后就注册了一个账号,把cooike拿下来,继续爬
2.博客园的新闻页面只有100页,每一页只有30条数据,经过10分钟所有的时间就吧数据爬完了。可能是我太笨没有找到后面的相关链接,然后就放弃在爬取了

InfoQ
可以爬取,但是如果爬去的时候频繁访问就会报错 http状态码:451 ,也不知道是哪里的问题。然后又放弃了。

IT之家
分析
首页:"https://www.ithome.com/"
1.我这里找了一个微软模块,"https://www.ithome.com/microsoft/" 开始爬
2.他这里是每页显示25条数据,然后点击加载更多 "https://www.ithome.com/category/collectionpage?domain=microsoft&ot时间戳",再显示25条,通过json形式返回。
加载更多:这里是通过查看他的html代码得到点击更多时回触发一个js方法,方法是一个ajax请求,请求的地址为 "步骤2",通过分析初步认为参数为当前数据最后一条的时间数据的时间戳.
3.得到对应的详情数据,然后通过分析得到想要的数据,灌到数据库
2020-08-24 上午
来公司第一件事情先看看数据灌的怎么样了。果然,果然报错了。。。,it之家的加载更多在执行到2000左右条数据后需要token。
然后注册用户拿到token只有修改了程序,继续重新开跑。。
目前2000多条数据了还没有报错。
2020-08-25 上午
下载12000条数据左右,爬虫停止了。。。
分析了下界面,还是要把所有的type都下载下来,不然数据不够多
https://www.ithome.com/category/collectionpage?domain=pc&ot=1598023865000 
https://www.ithome.com/category/collectionpage?domain=labs&ot=1594823765000
https://www.ithome.com/category/collectionpage?domain=video&ot=1587084731000
https://www.ithome.com/category/collectionpage?domain=original&ot=1587647957000
https://www.ithome.com/category/collectionpage?domain=apple&ot=1598139910000
https://www.ithome.com/category/collectionpage?domain=college&ot=1590744463000
https://www.ithome.com/category/collectionpage?domain=tu&ot=1596180223000
https://www.ithome.com/category/collectionpage?domain=live&ot=1589774350000
https://www.ithome.com/category/collectionpage?domain=5g&ot=1597895197000

https://mobile.ithome.com/category/collectionpage?domain=mobile&ot=1597973485000

https://next.ithome.com/category/domainpage?domain=next&subdomain=&ot=1596518633000
https://iphone.ithome.com/category/domainpage?domain=iphone&subdomain=&ot=1597973485000
https://auto.ithome.com/category/domainpage?domain=auto&subdomain=&ot=1597829976000
https://soft.ithome.com/category/domainpage?domain=soft&subdomain=&ot=1597825096000
https://digi.ithome.com/category/domainpage?domain=digi&subdomain=&ot=1598156972000
https://game.ithome.com/category/domainpage?domain=game&subdomain=&ot=1598057001000
https://win10.ithome.com/category/domainpage?domain=win10&subdomain=&ot=1597822777000
分为三部分,然后编写代码,
这次跑了15000条数据,然后将他部署到linux服务器上了,希望不要有问题。
2020-08-26 上午
昨天手机放到另一个屋子了,早上差30s迟到。。。公司9楼办公,直接2分钟爬上来的。。。
来了先开机,看看数据,已经10w+,终于吧数据灌进去了。这个就到这里了。

大致思路是这样的
我用到了 SpringBoot + mybatis-plus + WebMagic + jsoup + XPath

## 相关文档
参考链接:"https://blog.csdn.net/qq_36251958/article/details/79313035","https://blog.csdn.net/just4you/article/details/61197834?utm_source=blogxgwz4"
WebMagic:"http://webmagic.io/docs/zh/"
jsoup: "https://www.cnblogs.com/zhangyinhua/p/8037599.html"
XPath: "https://www.runoob.com/xpath/xpath-syntax.html"
去除转移
page.getHtml().xpath("//ul[@class='bl']//h2//a/@href")
page.getHtml().xpath("//ul[@class='bl']//div[@class='c']/@data-ot")

h.xpath("//h2//a/@href")
h.xpath("//div[@class='c']/@data-ot")

page.getHtml().xpath("//meta[@name='keywords']/@content") // 搜索关键字
page.getHtml().xpath("//title/text()") // 标题

String str = page.getHtml().xpath("//div[@class='post_content']").toString();
str = str.substring(str.indexOf(">"));
String info = str.substring(0,str.length()-6); // 内容
page.getHtml().xpath("//span[@id='source_baidu']//a/text()") // 来源
有不明白的可以私信一起探讨

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值