• 大小: 8KB
    文件类型: .py
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: Python
  • 标签: python  

资源简介

对神舟租车动态网页的爬虫。高效,且爬取的数据为结构化数据。不用后续进行清理

资源截图

代码片段和文件信息

#-*- coding: UTF-8 -*-

import sys
import os
import time
import urllib
import urllib2
import codecs
import requests
import numpy as np
import random
from collections import OrderedDict
from bs4 import BeautifulSoup
from openpyxl import Workbook
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import * 

reload(sys)
sys.setdefaultencoding(‘utf8‘)

def html_file_analysis(storeHtml):
    
    spider_list=[]
    bs=BeautifulSoup(open(storeHtml) ‘html.parser‘)
    for tag in bs.find_all(“tbody“):
        for child_tag in tag.contents:
            #print(“the child tag is “ child_tag)
            try:
                info=child_tag.find(“td“ class_=“info“)
                name=info.find(“p“ class_=“name“)
                other_info=info.find(“p“ class_=“oth“)

                ord_info=child_tag.find(“td“ class_=“ord“)
                pri_list = ord_info.select(‘em[class=num]‘)
                if len(pri_list)==1:
                    pri_list = ord_info.find_all(“em“ {‘class‘:“num od_chagst“})+pri_list
                if len(pri_list)==0:
                    pri_list = ord_info.find_all(“em“ {‘class‘:‘num od_full‘})
                
                assert(len(pri_list)==2)
                name=name.string.strip()
                other_info=other_info.string.strip()
                #print(name)

                price_per_day = pri_list[0].string.strip()
                price_total = pri_list[1].string.strip()
                #print(price_total)
                spider_list.append([name other_info price_per_day price_total])

            except:
                continue
            #finally:
    return spider_list

def html_analysis(page_source):
    
    spider_list=[]
    if page_source is None:
        return None
        

    bs=BeautifulSoup(page_source ‘html.parser‘)
    for tag in bs.find_all(“tbody“):
        for child_tag in tag.contents:
            #print(“the child tag is “ child_tag)
            try:
                info=child_tag.find(“td“ class_=“info“)
                name=info.find(“p“ class_=“name“)
                other_info=info.find(“p“ class_=“oth“)

                ord_info=child_tag.find(“td“ class_=“ord“)
                pri_list = ord_info.select(‘em[class=num]‘)
                if len(pri_list)==1:
                    pri_list = ord_info.find_all(“em“ {‘class‘:“num od_chagst“})+pri_list
                if len(pri_list)==0:
                    pri_list = ord_info.find_all(“em“ {‘class‘:‘num od_full‘})
                
                assert(len(pri_list)==2)
                name=name.string.strip()
                other_info=other_info.string.strip()
                #print(name)

                price_per_day = pri_list[0].string.strip()
                price_total = pri_list[1].string.strip()
                #print(price_total)
                spider_list.append([name other_info price_per_day price_total])

            except:
      

评论

共有 条评论