Python code example for grabbing NetEase news
这篇文章主要介绍了Python正则抓取网易新闻的方法,结合实例形式较为详细的分析了Python使用正则进行网易新闻抓取操作的相关实现技巧与注意事项,需要的朋友可以参考下
本文实例讲述了Python正则抓取网易新闻的方法。分享给大家供大家参考,具体如下:
自己写了些关于抓取网易新闻的爬虫,发现其网页源代码与网页的评论根本就对不上,所以,采用了抓包工具得到了其评论的隐藏地址(每个浏览器都有自己的抓包工具,都可以用来分析网站)
如果仔细观察的话就会发现,有一个特殊的,那么这个就是自己想要的了
然后打开链接就可以找到相关的评论内容了。(下图为第一页内容)
接下来就是代码了(也照着大神的改改写写了)。
#coding=utf-8 import urllib2 import re import json import time class WY(): def __init__(self): self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like '} self.url='http://comment.news.163.com/data/news3_bbs/df/B9IBDHEH000146BE_1.html' def getpage(self,page): full_url='http://comment.news.163.com/cache/newlist/news3_bbs/B9IBDHEH000146BE_'+str(page)+'.html' return full_url def gethtml(self,page): try: req=urllib2.Request(page,None,self.headers) response = urllib2.urlopen(req) html = response.read() return html except urllib2.URLError,e: if hasattr(e,'reason'): print u"连接失败",e.reason return None #处理字符串 def Process(self,data,page): if page == 1: data=data.replace('var replyData=','') else: data=data.replace('var newPostList=','') reg1=re.compile(" \[<a href=''>") data=reg1.sub(' ',data) reg2=re.compile('<\\\/a>\]') data=reg2.sub('',data) reg3=re.compile('<br>') data=reg3.sub('',data) return data #解析json def dealJSON(self): with open("WY.txt","a") as file: file.write('ID'+'|'+'评论'+'|'+'踩'+'|'+'顶'+'\n') for i in range(1,12): if i == 1: data=self.gethtml(self.url) data=self.Process(data,i)[:-1] value=json.loads(data) file=open('WY.txt','a') for item in value['hotPosts']: try: file.write(item['1']['f'].encode('utf-8')+'|') file.write(item['1']['b'].encode('utf-8')+'|') file.write(item['1']['a'].encode('utf-8')+'|') file.write(item['1']['v'].encode('utf-8')+'\n') except: continue file.close() print '--正在采集%d/12--'%i time.sleep(5) else: page=self.getpage(i) data = self.gethtml(page) data = self.Process(data,i)[:-2] # print data value=json.loads(data) # print value file=open('WY.txt','a') for item in value['newPosts']: try: file.write(item['1']['f'].encode('utf-8')+'|') file.write(item['1']['b'].encode('utf-8')+'|') file.write(item['1']['a'].encode('utf-8')+'|') file.write(item['1']['v'].encode('utf-8')+'\n') except: continue file.close() print '--正在采集%d/12--'%i time.sleep(5) if __name__ == '__main__': WY().dealJSON()
以上就是我爬取的代码了。
The above is the detailed content of Python code example for grabbing NetEase news. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
