首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >BeautifulSoup未获取web数据

BeautifulSoup未获取web数据
EN

Stack Overflow用户
提问于 2020-07-28 00:19:13
回答 1查看 58关注 0票数 1

我正在创建一个网络抓取器,以便从商会网站目录中提取一家公司的名称。

我正在使用BeautifulSoup。页面和soup对象看起来正常工作,但是当我抓取HTML内容时,返回一个空列表,而它应该用页面上的目录名填充。

尝试抓取的网页:https://www.austinchamber.com/directory

下面是HTML:

代码语言:javascript
运行
复制
<div>
  <ul> class="item-list item-list--small"> == $0
    <li>
      <div class='item-content'>
        <div class='item-description'>
          <h5 class = 'h5'>Women Helping Women LLC</h5>

以下是python代码:

代码语言:javascript
运行
复制
def pageRequest(url):
    page = requests.get(url)
    return page

def htmlSoup(page):
    soup = BeautifulSoup(page.content, "html.parser")
    return soup

def getNames(soup):
    name = soup.find_all('h5', class_='h5')
    return name

page = pageRequest("https://www.austinchamber.com/directory")
soup = htmlSoup(page)
name = getNames(soup)
for n in name:
    print(n)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-28 00:35:36

数据是通过Ajax动态加载的。要获取数据,可以使用以下脚本:

代码语言:javascript
运行
复制
import json
import requests


url = 'https://www.austinchamber.com/api/v1/directory?filter[categories]=&filter[show]=all&page={page}&limit=24'

page = 1

for page in range(1, 10):
    print('Page {}..'.format(page))
    data = requests.get(url.format(page=page)).json()

    # uncommentthis to print all data:
    # print(json.dumps(data, indent=4))

    for d in data['data']:
        print(d['title'])

打印:

代码语言:javascript
运行
复制
...

Indeed
Austin Telco Federal Credit Union - Taos
Green Bank
Seton Medical Center Austin
Austin Telco Federal Credit Union - Jollyville
Page 42..
Texas State SBDC - San Marcos Office
PlainsCapital Bank - Motor Bank
University of Texas - Thompson Conference Center
Lamb's Tire & Automotive Centers - #2 Research & Braker
AT&T Labs
Prosperity Bank - Rollingwood
Kerbey Lane Cafe - Central
Lamb's Tire & Automotive Centers - #9 Bee Caves
Seton Medical Center Hays
PlainsCapital Bank - North Austin
Ellis & Salazar Body Shop
aLamb's Tire & Automotive Centers - #6 Lake Creek
Rudy's Country Store and BarBQ

...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63119330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档