【问题标题】:How to extract data using beautiful soup如何使用美丽的汤提取数据
【发布时间】:2021-11-10 10:52:12
【问题描述】:
import requests
from bs4 import BeautifulSoup
import pandas as pd
baseurl='https://locations.atipt.com/'
headers ={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
}
r =requests.get('https://locations.atipt.com/al')
soup=BeautifulSoup(r.content, 'html.parser')
tra = soup.find_all('ul',class_='list-unstyled')
productlinks=[]
for links in tra:
    for link in links.find_all('a',href=True):
        comp=baseurl+link['href']
        productlinks.append(comp)

for link in productlinks:
    r =requests.get(link,headers=headers)
    soup=BeautifulSoup(r.content, 'html.parser')
    tag=soup.find_all('div',class_='listing content-card')
    for pro in tag:
        tup=pro.find('a',class_='name').find_all('p')
        for i in tup:
            print(i.get_text())

我正在尝试提取数据,但它们不会为我提供任何信息 我尝试从 p tag这些是我尝试从 p 标记中提取数据的页面检查它https://locations.atipt.com/al/alabaster

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    目前使用 css 选择器从 p 标签中获取数据的工作解决方案如下:

    import requests
    from bs4 import BeautifulSoup
    import pandas as pd
    baseurl = 'https://locations.atipt.com/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
    }
    r = requests.get('https://locations.atipt.com/al')
    soup = BeautifulSoup(r.content, 'html.parser')
    tra = soup.find_all('ul', class_='list-unstyled')
    productlinks = []
    for links in tra:
        for link in links.find_all('a', href=True):
            comp = baseurl+link['href']
            productlinks.append(comp)
    
    for link in productlinks:
        r = requests.get(link, headers=headers)
        soup = BeautifulSoup(r.content, 'html.parser')
        tag = ''.join([x.get_text(strip=True).replace('\xa0','') for x in soup.select('div.listing.content-card div:nth-child(2)>p')])
        print(tag)
    

    输出:

    634 1st Street NSte 100Alabaster, AL35007
    9256 Parkway ESte ABirmingham, AL352061940 28th Ave SBirmingham, AL352095431 Patrick WaySte 101Birmingham, AL35235833 St. Vincent's DrSte 100Birmingham, AL352051401 Doug Baker BlvdSte 104Birmingham, AL35242
    1877 Cherokee Ave SWCullman, AL350551301-A Bridge Creek Dr NECullman, AL35055
    1821 Beltline Rd SWSte BDecatur, AL35601
    4825 Montgomery HwySte 103Dothan, AL36303
    550 Fieldstown RdGardendale, AL35071323 Fieldstown Rd, Ste 105Gardendale, AL35071
    2804 John Hawkins PkwySte 104Hoover, AL35244
    700 Pelham Rd NorthJacksonville, AL36265
    1811 Hwy 78 ESte 108 & 109Jasper, AL35501-4081
    76359 AL-77Ste CLincoln, AL35096
    1 College DriveStation #14Livingston, AL35470
    106 6th Street SouthSte AOneonta, AL35121-1823
    50 Commons WaySte DOxford, AL36203
    301 Huntley PkwyPelham, AL35124
    41 Eminence WaySte BPell City, AL35128
    124 W Grand AveSte A-4Rainbow City, AL35906
    1147 US-231Ste 9 & 10Troy, AL36081
    7201 Happy Hollow RdTrussville, AL35173
    100 Rice Mine Road LoopSte 102Tuscaloosa, AL354061451 Dr. Edward Hillard DrSte 130Tuscaloosa, AL35401
    3735 Corporate Woods DrSte 109Vestavia, AL35242-2296
    636 Montgomery HwyVestavia Hills, AL352161539 Montgomery HwySte 111Vestavia Hills, AL35216
    

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 2015-10-21
      • 2020-01-19
      • 2021-04-03
      • 1970-01-01
      • 2015-05-08
      • 2018-09-13
      • 2021-05-24
      相关资源
      最近更新 更多