Reptile rampant forment le site de l'aéroport

Excel estimation main lose que j'écris série reptile rapide

site de l' aéroport taxi crawling
https://www.shanghaiairport.com/pdjc/jcjt/index_43742.html

objet Tag

  • tag.attrs
  • has_attr ()

Les noeuds enfants, les objets et le criblage de type chaîne encapsulé Tag BS4

  • Liste compréhensions utilisation
  • opération de liste pop ()
  • Liste tag.children iterator tag.contents

.get_text () .strings et .stripped_strings utilisation

  • get_text chaîne () dans son ensemble, et non pas l'élimination des espaces intérieurs
  • .strings
  • stripped_strings itérateur dans une liste de la liste ()
import numpy
import requests
import bs4
headers={'Accept':'text/html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
         }
response=requests.get("https://www.shanghaiairport.com/pdjc/jcjt/index_43742.html",headers=headers)
#print(response.text)
text=response.text
bs=bs4.BeautifulSoup(text,features='lxml')
#print(bs.prettify())
nodes=bs.find_all('tbody')


def has_attr_class(tag):
    return tag.has_attr('class')

def parse_table(node):
    #将子节点为bs4封装的字符串类对象过滤掉
    city=[child for child in node.children if child.string!=' ']
    array=[]
    for tr in city:
        if has_attr_class(tr):
            print("************出租车收费标准***************")
            print("表单字段名字为:")
            #根据tag中的标签,将tag对象中的string多个字符串,变为迭代器
            field=list(tr.stripped_strings)#去除空格和空行
            print(field,'\n')
            array.append(field)
            #field=tr.get_text().strip()
        else:
            value=list(tr.stripped_strings)
            #商圈和景点分开
            if len(value)==5:
                info=value.pop(0)
            print(value)
            array.append(value)
    print('----------------------------------------')
    return array

#原表中有两个表
#浦东机场一号航站楼和二号航站楼
import numpy
import requests
import bs4
headers={'Accept':'text/html',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
         }
response=requests.get("https://www.shanghaiairport.com/pdjc/jcjt/index_43742.html",headers=headers)
#print(response.text)
text=response.text
bs=bs4.BeautifulSoup(text,features='lxml')
#print(bs.prettify())
nodes=bs.find_all('tbody')


def has_attr_class(tag):
    return tag.has_attr('class')

def parse_table(node):
    #将子节点为bs4封装的字符串类对象过滤掉
    city=[child for child in node.children if child.string!=' ']
    array=[]
    for tr in city:
        if has_attr_class(tr):
            print("************出租车收费标准***************")
            print("表单字段名字为:")
            #根据tag中的标签,将tag对象中的string多个字符串,变为迭代器
            field=list(tr.stripped_strings)#去除空格和空行
            print(field,'\n')
            array.append(field)
            #field=tr.get_text().strip()
        else:
            value=list(tr.stripped_strings)
            #商圈和景点分开
            if len(value)==5:
                info=value.pop(0)
            print(value)
            array.append(value)
    print('----------------------------------------')
    return array

#原表中有两个表
#浦东机场一号航站楼和二号航站楼
global i
i=1
for node in nodes:
    array=parse_table(node)
    numpy.savetxt(r"E:\360MoveData\Users\hzsdl\Desktop\%d.txt" % i,array,delimiter=',',fmt='%s')
    i+=1

Je suppose que tu aimes

Origine www.cnblogs.com/lzycodinglife/p/12584851.html
conseillé
Classement