Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Beautiful Soup: Extract text at the a anchor after url

I have some html where the URL in the a href comes before the title that would appear on the page. I am trying to get at that title and extract that into a data frame/list. The following code is what I have so far.

import requests
from bs4 import BeautifulSoup

url = 'https://patentsview.org/download/data-download-tables'
page = requests.get(url)

soup = BeautifulSoup(page.content, "html.parser")

results = soup.find_all("div", class_="file-title")
print(results)

I would like the results to be in the following format:

Title
application
assignee

I was following this page on Real Python but I have have come to a standstill since I cannot seem to translate their next part into my needs.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help with this would be wonderful. Thank you in advance for your help.

>Solution :

Just call .text on the <a> in each of the <div> to print your information:

for e in soup.find_all("div", class_="file-title"):
    print(e.a.text)

or with css selector:

for a in soup.select('.file-title a'):
    print(a.text)

Example

import requests
from bs4 import BeautifulSoup
import pandas as pd

url = 'https://patentsview.org/download/data-download-tables'
page = requests.get(url)

soup = BeautifulSoup(page.content, "html.parser")

for e in soup.find_all("div", class_="file-title"):
    print(e.a.text)
Output
application
assignee
botanic
cpc_current
cpc_group
cpc_subgroup
cpc_subsection
figures
...

Or as DataFrame

pd.DataFrame([a.text for a in soup.select('.file-title a')], columns=['Title'])
Output:
Title
application
assignee
botanic
cpc_current
cpc_group
cpc_subgroup
cpc_subsection
figures
foreigncitation
foreign_priority
government_interest
government_organization
inventor
ipcr
lawyer
location
mainclass
mainclass_current
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading