Use Python to crawl embarrassing encyclopedia users and jokes

Source: Internet
Author: User

Recently learning Python crawler, the crawler can do a lot of interesting things, this article uses Python crawler to crawl the embarrassing encyclopedia of users and jokes, we need to use Python to get embarrassing Wikipedia page users and jokes, we need to match two times, and then the obtained content formatted output can be.
This is the script I wrote:

#coding:utf-8import urllib2import urllibimport reimport sysreload(sys)sys.setdefaultencoding(‘utf8‘)hearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"##糗事百科的页面链接url = "https://www.qiushibaike.com/text/"##创建opener对象opener = urllib2.build_opener()opener.addheaders = [hearders]urllib2.install_opener(opener)##存储页面的源代码data = urllib2.urlopen(url).read().decode("utf-8")##下面是正则匹配表达式,par1匹配的是用户,par2匹配的是段子。par1 = ‘(

Take a look at the effect of executing the script directly.

[[email protected] python]# python qiushi.py 用户0:长弓射飞机段子:打麻将三缺一,call发小筒子过来了(胖的像麻将里的一筒)。<br/>筒子: 我刚做完头发准备回家,这样不好吧!<br/>小八: 我们一起帮你作证怕毛线。<br/>。。。。<br/>筒子手气不错,大杀四方。。。。<br/>筒子: 不好意思!不好意思!手气有点好!各位我也不想,手一边拿钱往自己口袋塞,一边贱笑。<br/>来电话了!来电话了!筒子: 喂!老婆我刚做完头发,在和小八他们吃宵夜!你要不要来吃?哦!不来了啊!那拜拜!<br/>叮叮叮。。。小八: 喂嫂子,我在做头发啊!没看到筒子啊!<br/><br/>来电话了!来电话了!筒子: 喂!喂!喂!!!!!!!!用户1:红红的辣辣椒段子:跟朋友去吃火锅,人挺多的,过了一会儿,服务员过来问有没有漏什么菜,,我说还有一盘菜没上,服务员说那个,,<br/>“羊尾巴”<br/>噗…哈哈…一屋子狂笑,,<br/>服务员懵逼的说“没这个呀,,”<br/>我旁边的哥们儿说“羊 鞭。。”用户2:倔强的傻晴°段子:下午大扫除,我说我腰好酸痛。经理说他腰也疼。我说我是生孩子剖腹产打麻药的后遗症。你难道也生过孩子?<br/>他说以前手术打了两次麻药,一针是阑尾炎手术。第二针是因为第一针割阑尾的时候割错了,所以又重新割了一遍。<br/>我脑抽问了一句:“第一次是把丁丁割了吗?”......

Because too much content, only part of the output.

You can also save the contents of the output to a TXT file

[[email protected] python]# python qiushi.py > qiushi.txt[[email protected] python]# cat qiushi.txt 用户0:长弓射飞机段子:打麻将三缺一,call发小筒子过来了(胖的像麻将里的一筒)。<br/>筒子: 我刚做完头发准备回家,这样不好吧!<br/>小八: 我们一起帮你作证怕毛线。<br/>。。。。<br/>筒子手气不错,大杀四方。。。。<br/>筒子: 不好意思!不好意思!手气有点好!各位我也不想,手一边拿钱往自己口袋塞,一边贱笑。<br/>来电话了!来电话了!筒子: 喂!老婆我刚做完头发,在和小八他们吃宵夜!你要不要来吃?哦!不来了啊!那拜拜!<br/>叮叮叮。。。小八: 喂嫂子,我在做头发啊!没看到筒子啊!<br/><br/>来电话了!来电话了!筒子: 喂!喂!喂!!!!!!!!用户1:红红的辣辣椒段子:跟朋友去吃火锅,人挺多的,过了一会儿,服务员过来问有没有漏什么菜,,我说还有一盘菜没上,服务员说那个,,<br/>“羊尾巴”<br/>噗…哈哈…一屋子狂笑,,<br/>服务员懵逼的说“没这个呀,,”<br/>我旁边的哥们儿说“羊 鞭。。”用户2:倔强的傻晴°段子:下午大扫除,我说我腰好酸痛。经理说他腰也疼。我说我是生孩子剖腹产打麻药的后遗症。你难道也生过孩子?<br/>他说以前手术打了两次麻药,一针是阑尾炎手术。第二针是因为第一针割阑尾的时候割错了,所以又重新割了一遍。<br/>我脑抽问了一句:“第一次是把丁丁割了吗?”......

It is important to note that the regular expression of the crawler script is not always able to match, after all, the source code of other people's pages will change, so you need to deal with the actual page source to write the corresponding regular expression.

Use Python to crawl embarrassing encyclopedia users and jokes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.