Beautifulsoup - 刮网页 - 动态加载页面

时间:2017-08-10 14:45:25

标签: python parsing web-scraping beautifulsoup screen-scraping

我想抓一个网页:https://www.justdial.com/Mumbai/Dairy-Product-Retailers-in-Thane/nct-10152687

我需要所有商店名称,电话号码及其地址

的数据

但我只能做到10岁 导致加载滚动网页所需的其他项目

我的代码:

import requests
import bs4

crawl_url = requests.get('https://www.justdial.com/Mumbai/Dairy-Product-
Retailers-in-Thane/nct-10152687', headers={'User-Agent': 'Mozilla/5.0'})
crawl_url.raise_for_status()


soup = bs4.BeautifulSoup(crawl_url.text, 'lxml')

for elems in soup.find_all('span', class_="jcn"):
    select_a = elems.select('a')
    for links in select_a:
        href = links.get('href')
        res = requests.get(href, headers={'User-Agent': 'Mozilla/5.0'})

        xsoup = bs4.BeautifulSoup(res.text, 'lxml')

        Name = xsoup.select('.fn')
        tel = xsoup.select('.tel')
        add = xsoup.select('.adrstxtr')
        a = Name[0]
        b = tel[0]
        c = add[0]
        print(a.getText())
        print("--"*10)
        print(b.getText())
        print("--"*10)
        print(c.getText())
        print("=="*25)

当我们向下滚动页面时,其他项目加载 所以我想知道如何获得我想要的任何数据/项目

我尝试了this

但是没有安静地理解嘛,我也没有那个POST方法:/

如果需要更多信息告诉我

1 个答案:

答案 0 :(得分:2)

t.m.adam给出的解决方案 这是代码

import requests
import bs4

def spider(max_pages):
    page = 1
    while page < max_pages:
        url = "https://www.justdial.com/Mumbai/Dairy-Product-Retailers-in-
Thane/nct-10152687/page-%s" % page
        crawl_url = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
        crawl_url.raise_for_status()
        soup = bs4.BeautifulSoup(crawl_url.text, 'lxml')
        for elems in soup.find_all('span', class_="jcn"):
            select_a = elems.select('a')
            for links in select_a:
               href = links.get('href')
               res = requests.get(href, headers={'User-Agent': 
'Mozilla/5.0'})
                xsoup = bs4.BeautifulSoup(res.text, 'lxml')
                Name = xsoup.select('.fn')
                tel = xsoup.select('.tel')
                add = xsoup.select('.adrstxtr')
                a = Name[0]
                b = tel[0]
                c = add[0]
                print(a.getText())
                print("--"*10)
                print(b.getText())
                print("--"*10)
                print(c.getText())
                print("=="*25)
        page += 1


spider(3)