简体   繁体   中英

Scraping certain attribute - Beautiful Soup Python

I need help scraping the words "CPCAdvertising.com" within the highlighted span tags (see attached screenshot of the HTML). I am unsure of how to iterate through properly. Here is what I have so far:

import requests
from bs4 import BeautifulSoup
page_number = 1
flippa_page = requests.get('https://www.flippa.com/search?filter[property_type]=domain&filter[status]=won&filter[sale_method]=auction&page[number]={}&page[size]=250'.format(page_number))
price_list = []
domain_list = []
for i in range(120):
    src = flippa_page.content
    soup = BeautifulSoup(src, 'lxml')
    for span_tag in soup.find_all('span'):
        domain_list.append(span_tag.attrs['class'])
    page_number += 1

HTML Screenshot

Since your URL doesn't work for me, I'm using a different one from the same website. Regardless, you can specify the class within the find_all() command like so:

import requests
from bs4 import BeautifulSoup

flippa_page = requests.get('https://flippa.com/10339489-e-commerce-sports-and-outdoor')
src = flippa_page.content
soup = BeautifulSoup(src, 'lxml')

for s in soup.find_all('span', {'class': 'ListingList-itemPrice'}):
    # Print out the text within the tag
    print(s.text.strip())

The words should be in in span_tag.string .

Using html.parser instead of lxml i could find the span tags:

for item in soup.find_all('span'): 
     if (str(item.contents).find('CPCAdvertising.com')) > -1: 
         print(item) 

<span class="Basic___propertyName">CPCAdvertising.com</span>
<span class="Basic___title">CPCAdvertising.com - One Dollar Reserve !!</span>

I wasn't able to parse with lxml for some reason, if you can give me a tip on what lxml library your using I can check with it

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