hxy。。 2021-04-26 13:53 采纳率: 63.6%
浏览 38
已结题

python爬虫问题,BeautifulSoup提问,真心求教,急

目的是爬取北京到上海机票预订 - 北京到上海机票预约 - 同程机票预订 (ly.com)网站中的机票信息。设计使用BeautifulSoup库中的find_all()函数先获取所有 <div class="flight-item-head data-v-13439d30"> 标签的信息,之后使用正则表达式获取更加细致的信息。但是,在第一步find_all()函数的使用后,没有获取网页中所有符合要求div标签的内容,想知道是为什么。

import urllib.error
import urllib.request
from tkinter import *

from bs4 import BeautifulSoup


def checkChinese(InPut):
    flag = False
    for i in range(0, len(InPut)):
        if ord(InPut[i]) <= 255:
            flag = True
            break
    if flag is False:
        return True
    else:
        return False


def checkDigit(InPut):
    return InPut.isdigit()


def askForUrl(url):
    head = {'User-Agent': 'Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / '
                          '80.0 3987.122 Safari / 537.36'}
    request = urllib.request.Request(url, headers=head)
    html = ''
    try:
        response = urllib.request.urlopen(request)
        html = response.read().decode('utf-8')
    except urllib.error.URLError as e:
        if hasattr(e, 'code'):
            print(e.code)
        if hasattr(e, 'reason'):
            print(e.reason)
    return html


def setUrl(baseurl, locDict):
    depLocation = str(e1.get())
    ariLocation = str(e2.get())
    depLocation = str(locDict[depLocation])[2:5]
    ariLocation = str(locDict[ariLocation])[2:5]
    ariYear = str(e3.get())
    ariMonth = str(e4.get())
    ariDay = str(e5.get())
    url = baseurl + depLocation + '-' + ariLocation + '?' + 'date=' + ariYear + '-' + ariMonth + '-' + ariDay
    return url


def searchForBaseInfo(baseurl1, locDict1, flightNameList1, depTime1, ariTime1, depAirport1, ariAirport1, ifAddDays1):
    url1 = setUrl(baseurl1, locDict1)
    # print(url1)
    html = askForUrl(url1)
    soup = BeautifulSoup(html, 'lxml')
#这里####################
    for item in soup.find_all('div', class_='flight-item'):
#这里####################
        item = str(item)
        print(item)
        flightNameList1.append(re.findall(findFlightName, item)[0])
        tempTime = re.findall(findDepTime, item)[0][0] + ':' + re.findall(findDepTime, item)[0][1]
        depTime1.append(tempTime)
        tempTime = re.findall(findAriTime, item)[0][0] + ':' + re.findall(findAriTime, item)[0][1]
        ariTime1.append(tempTime)
        depAirport1.append(re.findall(findDepAirport, item)[0])
        ariAirport1.append(re.findall(findAriAirport, item)[0])
        tempAddDays = re.findall(findIfAddDays, item)
        if len(tempAddDays) == 0:
            ifAddDays1.append('n')
        else:
            ifAddDays1.append(tempAddDays[0])



  • 写回答

1条回答 默认 最新

  • 木三136 2021-04-26 14:31
    关注

    首先在使用爬虫前 您需要确保爬取的页面数据是全部的 即不存在动态加载的情况

    若有的数据是动态加载的 您还需要爬取网页所携带的的json文件

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 9月29日
  • 已采纳回答 9月21日

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!