zoukankan      html  css  js  c++  java
  • python 爬取网页内容

     1 #encoding:UTF-8
     2 import urllib
     3 import urllib.request
     4 import bs4
     5 from bs4 import BeautifulSoup as bs
     6 def test1():
     7     url = "http://www.stylusstudio.com/edifact/D95B/CODECO.htm"
     8     resp = urllib.request.urlopen(url)
     9     data = resp.read().decode('UTF-8')
    10     soup = bs(data, 'html.parser')    
    11     segment11= soup.find_all('table')
    12     segment1=segment11[7].find_all('tr')#表示第几个table,此时表示进去html网页中的第7个table
    13 
    14  
    15     f2=open('./text1.txt','a',encoding='cp852')
    16     for item in segment1:
    17 
    18             print(item)
    19             '''
    20             <tr class="FrameTreeFont"><td><span class="FrameDrawFont">│
    21             <span class="FrameHideFont">─</span>│<span class="FrameHideFont">─</span>├─</span>
    22             <a class="FrameItemFont" href="DAM_.htm" target="classFrame" title="Damage">DAM</a> 
    23             Damage</td><td align="right"><span class="FrameDetailFont"> ×1 
    24             </span></td><td><span class="FrameDetailFont">(M)</span></td></tr>
    25             '''
    26 
    27             print(item.get_text())#以文本方式呈现
    28             '''
    29             │─│─├─DAM Damage ×1 (M)
    30             '''
    31             # print(item.td.span.get_text())#获取具体标签内部内容
    32             print([text for text in item.stripped_strings] )#以列表方式呈现
    33             '''
    34             ['│', '─', '│', '─', '├─', 'DAM', 'Damage', '×1', '(M)']
    35             '''
    36             '''
    37             soup.get_text("|")#u'
    I linked to |example.com|
    '进一步,通过strip去除掉文本每个位的头尾空白。
    38 
    39             soup.get_text("|", strip=True)#u'I linked to|example.com'
    40             '''
    41             f2.writelines(str([text for text in item.stripped_strings])+'
    ')
    42     f2.close()     
    43 if __name__=='__main__':
    44     test1()
  • 相关阅读:
    css固定元素位置(fixed)
    【转】php 下载保存文件保存到本地的两种实现方法
    【转】关于URL编码/javascript/js url 编码/url的三个js编码函数
    IE6对png图片的处理
    用phpcms开发模块时中文乱码问题
    【转】MySQL数据类型和常用字段属性总结
    PHPCMS几个有用的全局函数
    PHPCMS系统常量
    PHPCMS系统使用的弹出窗口插件artDialog
    基础知识点(一)
  • 原文地址:https://www.cnblogs.com/smuxiaolei/p/7417384.html
Copyright © 2011-2022 走看看