简体   繁体   中英

Scraping data using BeautifulSoup

I'm trying scrape the data into a dictionary from this site,

from bs4 import BeautifulSoup 
import requests 
from pprint import pprint

page = requests.get('https://webscraper.io/') 
soup = BeautifulSoup(page.text, "lxml")

info = []
for x in range(1,7):
    items = soup.findAll("div",{"class":f"info{x}"})
    info.append(items)

however, the HTML tags are not being removed.

Something like this might work? (Replace the webscraper.io url with your actual request URL; Also, you'd still need to clean up the \\n characters from the output):

from bs4 import BeautifulSoup 
import requests 
from pprint import pprint

page = requests.get('https://webscraper.io/') 
soup = BeautifulSoup(page.text, "lxml")

info = []
for x in range(1,7):
    items = soup.findAll("div",{"class":f"info{x}"})
    info += [item.text for item in items]

Ie item.text, and concatenate the resulting array with info

You need to use .text . Then to get in the way you want, would need to do a bit of string manipulation.

from bs4 import BeautifulSoup 
import requests 
from pprint import pprint

url = 'https://webscraper.io/'
page = requests.get(url) 
soup = BeautifulSoup(page.text, "lxml")


info = []
for x in range(1,7):
    item = soup.find("div",{"class":"info%s" %x}).text.strip().replace('\n',': ')
    info.append(item)

info = '\n'.join(info)
print (info)

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