Skip to content

Instantly share code, notes, and snippets.

@makesomelayouts
Last active July 4, 2023 14:45
Show Gist options
  • Select an option

  • Save makesomelayouts/7374f0b4845e857964f1df4ea083381f to your computer and use it in GitHub Desktop.

Select an option

Save makesomelayouts/7374f0b4845e857964f1df4ea083381f to your computer and use it in GitHub Desktop.
import requests
from fake_useragent import UserAgent
response = requests.get('https://httpbin.org/get', headers={'user-agent': UserAgent().random})
print(response.status_code)
print(f'response.content={response.content}\n')
print(f'response.headers={response.headers}\n')
print(f'response.text={response.text}\n')
print(f'response.json()={response.json()}\n')
# download video:
# with open('1.webm', 'wb') as f:
# for chunk in response.iter_content(chunk_size=1024 * 100):
# f.write(chunk)
from bs4 import BeautifulSoup
soup = BeautifulSoup(response.content, 'lxml')
# h1 = soup.h1
# print(h1.attrs['id'])
# print(h1['id'])
# print(h1.get('id')) # +
# print(h1.text(strip=True, separator=' '))
# find() # find_all('a', class_='p-2')
print(soup.a)
print(soup.find('nav').find_all('a')) # +
print(soup.select_one('nav').select('a'))
# <span data-name>The Sad Onion</span> ==
# find('span', {'data-name' : True})
# import soupsieve as sv
# links = sv.select('nav span', soup)
# links = sv.select('nav > span', soup)
# links = sv.select('nav a::is(.test1, .test2)', soup)
# links = sv.select('nav a::not(.test1, .test2)', soup)
# select('table.table tbody tr td:last-child')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment