简体   繁体   中英

How do you loop the URL output for BeautifulSoup?

So I want to extract each block of data, with a space in between, right now I have the loop setup but when I go to print it is only extracting the second player profile. Any idea how I can fix this?

If it was working right the output would be:

Greg Oden C  #20
Born: Jan 22, 1988 (33 years old)
Birthplace/Hometown: Buffalo, New York
Nationality: United States
Height: 7-0 (213cm)     Weight: 273 (124kg)
Website: http://www.gregoden52.com/
Current NBA Status: Unrestricted Free Agent
Agent: Bill Duffy
Draft Entry: 2007 NBA Draft
Early Entry Info: 2007 Early Entrant
Drafted: Round 1, Pick 1, Portland Trail Blazers
Pre-Draft Team: Ohio State (Fr)
High School: Lawrence North High School [Indianapolis, Indiana]
AAU Team: Spiece Indy Heat

Carl Landry F
Current Team: N/A
Born: Sep 19, 1983 (37 years old)
Birthplace/Hometown: Milwaukee, Wisconsin  
Nationality: United States
Height: 6-9 (206cm)     Weight: 248 (112kg)
Hand: Right
Website: https://carllandry.com/
@CarlLandry
Current NBA Status: Unrestricted Free Agent
Agent: Mark Bartelstein, Reggie Brown
Draft Entry: 2007 NBA Draft
Drafted: Round 2, Pick 1, Seattle SuperSonics
Draft Rights Trade: SEA to HOU, Jun 28, 2007
Pre-Draft Team: Purdue (Sr)
High School: Vincent High School [Milwaukee, Wisconsin]

Here is the code:

import csv ;import requests
from bs4 import BeautifulSoup
import csv
import re

url_list = ['https://basketball.realgm.com/player/player/Summary/1',
            'https://basketball.realgm.com/player/player/Summary/2']

for url in url_list:
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

player = soup.find_all('div', class_= 'wrapper clearfix container')[0]


playerprofile = re.sub(r'\n\s*\n', r'\n', player.get_text().strip(), flags=re.M)

print(playerprofile)
import csv
import requests
from bs4 import BeautifulSoup
import csv
import re

url_list = ['https://basketball.realgm.com/player/player/Summary/1',
            'https://basketball.realgm.com/player/player/Summary/2']

for url in url_list:
    r = requests.get(url)
    soup = BeautifulSoup(r.text, 'html.parser')

    player = soup.find_all('div', class_='wrapper clearfix container')[0]

    playerprofile = re.sub(
        r'\n\s*\n', r'\n', player.get_text().strip(), flags=re.M)

    print(playerprofile + "\n")

This code is working as shown in your desired output, it seems that the parsing and printing of the player in your code occurs after the loop is completed. It should be done for each iteration of the loop, so you can just indent it into the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM