【问题标题】:Scrape Dynamic Site not returning specific href beautiful soup抓取动态网站不返回特定的 href 美汤
【发布时间】:2020-10-10 22:46:47
【问题描述】:

我一直在尝试用 selenium 和 beautifulSoup 刮掉这个site。但是我认为我做错了什么,因为我无法从特定类('card-content)返回href。

所以我用了这个:

links = [link.get('href') for link in soup.select('a')]

这带来了几个对我来说无关紧要的结果。我想要所有以'/portal..'开头的href,这会导致后期工作。

我想出的代码如下:

links = []
for i in range(1,510):
    driver = webdriver.Chrome('../chromedriver')
    url = "https://www.reclameaqui.com.br/empresa/portal-da-prefeitura-de-sao-paulo/lista-reclamacoes/?pagina=%s" % i
    driver.get(url)
    soup = BeautifulSoup(driver.page_source)
    href = [a['href'] for a in soup.select('a[href]')]
    if len(href) in ('321','323','324','325','326'):
        for j in range(262,272):
            links.append(href[j])
    elif len(href) in ('330','331'):
        for j in range(264,274):
            links.append(href[j])
    else:
        for j in range(258,274):
            links.append(href[j])    
    driver.close()
    time.sleep(2)

“if-else”是为了尝试从它返回的 href 列表中获取正确的 href,但由于某种原因,它只激活 else 语句,之后我仍然需要进行一些清理。

有一种方法可以只从 div 1234562 获取我想要的 href(href="/portal-da-prefeitura-de-sao-paulo/som-alto-de-propaganda-com-autos-falantes_Jg3fS46He5aLo9PZ/") ="卡片内容"?

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: python selenium web-scraping dynamic beautifulsoup


    【解决方案1】:

    请检查:

    webPageSource = driver.page_source
    
    soup = BeautifulSoup(webPageSource, "html.parser")
    
    links_ = soup.findAll('a',{'class':'link-complain-id-complains label-not-answered'})
    
    for i in range(0,len(links_)):
    
         print(links_[i]['href'])
    

    【讨论】:

    • 使用 find_all() 确实是比替换更好的选择。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2021-03-30
    • 2020-07-08
    • 2021-12-06
    相关资源
    最近更新 更多