How does python capture the content of embarrassing encyclopedia?
This article mainly explains "how python captures the content of embarrassing encyclopedia". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "python how to capture the content of embarrassing encyclopedia".
# capture the contents of the embarrassing encyclopedia import requestsfrom lxml import etreeclass Qiushi (): def _ _ init__ (self): self.url = 'http://www.qiushibaike.com/8hr/page/{}'; self.headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1 Trident/5.0 "} Def parse_url (self, url): response = requests.get (url, timeout = 10, headers = self.headers) assert response.status_code = = 200print (url) return etree.HTML (response.text) def parse_content (self Html): item = html.xpath ('/ / div [@ class= "recommend-article"] / ul/li') print (item) for i in item: # content print (i.xpath ('. / div/a [@ class= "recmd-content"] / text ()') # funny number print ('. / div/div [@ class= "recmd-detail clearfix"] / div/span [1] / text ()') # comments number print ('. / div/div [@ class= "recmd-detail clearfix"] / div/span [4] / text ()) # user name print (i.xpath ('. / div/div [@ class= "recmd-detail clearfix"] / a/span/text ()') # avatar address print (i.xpath ('. / div/div [@ class= "recmd-) Detail clearfix "] / a _ def run (self): url = self.url.format (1) Html = self.parse_url (url); self.parse_content (html); if _ name__ = ='_ main__': qiu = Qiushi (); qiu.run () Thank you for your reading. The above is the content of "how python captures the content of embarrassing encyclopedia". After the study of this article, I believe you have a deeper understanding of how python captures the content of embarrassing encyclopedia, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!