Skip to content

Instantly share code, notes, and snippets.

@misirov
Created October 11, 2023 17:05
Show Gist options
  • Save misirov/c4bd6cee72df7b1e81b7b4686b125525 to your computer and use it in GitHub Desktop.
Save misirov/c4bd6cee72df7b1e81b7b4686b125525 to your computer and use it in GitHub Desktop.
EtherscanLabelScraping
user_agents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko"
]
def get_label_name(address):
url = f"https://etherscan.io/address/{address}"
# Etherscan blocks python agents so need to change user agent
headers = {
'User-Agent': random.choice(user_agents)
}
response = r.get(url, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'html.parser')
# Find all relevant HTML elements by using the inspector
label_element = soup.find_all('span', {'class': 'hash-tag text-truncate'})
if label_element:
return label_element[0].text.strip()
else:
return None
test_address = "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0"
label_name = get_label_name(test_address)
if label_name:
print(f"Label name for address {test_address}: {label_name}")
else:
print(f"No label name")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment