Clearing Python HTML

This is actually not scratched, I'm just trying to find the URLs on a webpage where the class has a specific meaning. For example:

<a class="myClass" href="/url/7df028f508c4685ddf65987a0bd6f22e">

I want to get the href value. Any ideas how to do this? Maybe a regular expression? Could you post some sample code? I guess html scraping libs such as BeautifulSoup are a bit overkill just for this ...

Thank you very much!

+3
source share
7 answers

Regex is usually a bad idea, try using BeautifulSoup

Quick example:

html = #get html
soup = BeautifulSoup(html)
links = soup.findAll('a', attrs={'class': 'myclass'})
for link in links:
    #process link
+16
source

Aargh, HTML!

, Python BeautifulSoup lxml, .

+9

Regex . HTML . ?

+2

Regex HTML. :)

+1 BeautifulSoup.

+1

, ( )

f=open("htmlfile")
for line in f:
    if "<a class" in line and "myClass" in line and "href" in line:
        s = line [ line.index("href") + len('href="') : ]
        print s[:s.index('">')]
f.close()

HTML- .

+1

The thing is, I know the structure of the HTML page, and I just want to find this type of link (where class = "myclass"). BeautifulSoup anyway?

0
source

Source: https://habr.com/ru/post/1723826/


All Articles