|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup |
| 3 | + |
| 4 | +proxies = { |
| 5 | + "http": "http://127.0.0.1:2080", |
| 6 | + "https": "http://127.0.0.1:2080", |
| 7 | +} |
| 8 | + |
| 9 | +def test_proxy(): |
| 10 | + test_url = "https://httpbin.org/ip" |
| 11 | + try: |
| 12 | + response = requests.get(test_url, proxies=proxies) |
| 13 | + if response.status_code == 200: |
| 14 | + print(f"Proxy is working! Response: {response.json()}") |
| 15 | + return True |
| 16 | + else: |
| 17 | + print(f"Failed to get a valid response. Status code: {response.status_code}") |
| 18 | + return False |
| 19 | + except requests.exceptions.RequestException as e: |
| 20 | + print(f"An error occurred while testing the proxy: {e}") |
| 21 | + return False |
| 22 | + |
| 23 | +def scrape_hacker_news(): |
| 24 | + url = "https://news.ycombinator.com/" |
| 25 | + |
| 26 | + try: |
| 27 | + response = requests.get(url, proxies=proxies) |
| 28 | + if response.status_code == 200: |
| 29 | + print("Successfully fetched Hacker News!") |
| 30 | + soup = BeautifulSoup(response.content, 'html.parser') |
| 31 | + |
| 32 | + submissions = soup.find_all('tr', class_='athing') |
| 33 | + |
| 34 | + for submission in submissions: |
| 35 | + rank = submission.find('span', class_='rank').text.strip('.') |
| 36 | + title_element = submission.find('span', class_='titleline').find('a') |
| 37 | + title = title_element.text if title_element else 'No Title' |
| 38 | + link = title_element['href'] if title_element else '#' |
| 39 | + score = submission.find('span', class_='score').text if submission.find('span', class_='score') else 'No Score' |
| 40 | + user = submission.find('a', class_='hnuser').text if submission.find('a', class_='hnuser') else 'Anonymous' |
| 41 | + age = submission.find('span', class_='age').text if submission.find('span', class_='age') else 'Unknown' |
| 42 | + |
| 43 | + print(f"Rank: {rank}") |
| 44 | + print(f"Title: {title}") |
| 45 | + print(f"Link: {link}") |
| 46 | + print(f"Score: {score}") |
| 47 | + print(f"User: {user}") |
| 48 | + print(f"Age: {age}") |
| 49 | + print('-' * 40) |
| 50 | + else: |
| 51 | + print(f"Failed to fetch Hacker News. Status code: {response.status_code}") |
| 52 | + except requests.exceptions.RequestException as e: |
| 53 | + print(f"An error occurred while scraping: {e}") |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + if test_proxy(): |
| 57 | + scrape_hacker_news() |
| 58 | + else: |
| 59 | + print("Proxy is not working. Exiting...") |
0 commit comments