码迷,mamicode.com
首页 > 其他好文 > 详细

easyspider

时间:2018-10-18 22:06:26      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:net   pen   ==   .net   put   attribute   bs4   error:   ant   

# -*- coding: utf-8 -*-
"""
Created on Fri Aug 18 15:58:13 2017
@author: JClian
"""
import re
import bs4
import urllib.request  
from bs4 import BeautifulSoup 
import urllib.parse
import sys

search_item = input("Enter what you want(Enter ‘out‘ to exit):")
while search_item != out:
    if search_item == out:
        exit(0)
    print("please wait...")
    try:
        url = https://baike.baidu.com/item/+urllib.parse.quote(search_item)
        html = urllib.request.urlopen(url)  
        content = html.read().decode(utf-8)
        html.close()
        soup = BeautifulSoup(content, "lxml")  
        text = soup.find(div, class_="lemma-summary").children
        print("search result:")
        for x in text:
            word = re.sub(re.compile(r"<(.+?)>"),‘‘,str(x))
            words = re.sub(re.compile(r"\[(.+?)\]"),‘‘,word)
            print(words,\n)
    except AttributeError:
        print("Failed!Please enter more in details!")
    search_item = input("Enter what you want(Enter ‘out‘ to exit):")
--------------------- 
作者:剑与星辰 
来源:CSDN 
原文:https://blog.csdn.net/jclian91/article/details/77387842 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

easyspider

标签:net   pen   ==   .net   put   attribute   bs4   error:   ant   

原文地址:https://www.cnblogs.com/sky-ai/p/9813126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!