-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (78 loc) · 3.39 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import json
import requests
import time
from bs4 import BeautifulSoup
HEADERS = {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}
URL = "https://hltv.org"
def gethtmlandcontent():
r = requests.get(URL, headers=HEADERS)
soup = BeautifulSoup(r.text, 'html.parser')
newsline_first = soup.find("div", class_="standard-box standard-list").find_all("a", class_="newsline article")
urls = []
news_dictionary = {}
for news in newsline_first:
news_url = URL + news.get("href")
news_id = news_url.split("/")[-2]
urls.append(news_url)
for url in urls:
noway = requests.get(url, headers=HEADERS).text
soup2 = BeautifulSoup(noway, "html.parser")
news_title = soup2.find("h1", class_="headline").text.strip()
news_description = soup2.find("p", class_="headertext").text.strip()
news_datetime = soup2.find("div", class_="date").text
news_dictionary[news_id] = {
"news_title": news_title,
"news_url": news_url,
"news_description": news_description,
"news_datetime": news_datetime
}
time.sleep(1)
# print(news_dict)
with open("news_dictionary.json", "w", encoding="UTF-8") as file:
json.dump(news_dictionary, file, indent=4, ensure_ascii=False)
def newnews():
with open("news_dictionary.json", encoding="UTF-8") as file:
news_dict = json.load(file)
r = requests.get(URL, headers=HEADERS)
soup = BeautifulSoup(r.text, 'html.parser')
newsline_first = soup.find("div", class_="standard-box standard-list").find_all("a", class_="newsline article")
urls = []
fresh_dictionary = {}
for news in newsline_first:
news_url = URL + news.get("href")
news_id = news_url.split("/")[-2]
urls.append(news_url)
if news_id in news_dict:
continue
else:
for url in urls:
noway = requests.get(url, headers=HEADERS).text
soup2 = BeautifulSoup(noway, "html.parser")
news_title = soup2.find("h1", class_="headline").text.strip()
news_description = soup2.find("p", class_="headertext").text.strip()
news_datetime = soup2.find("div", class_="date").text
# news_first_image = soup2.find("img", class_="image").get("src")
news_dict[news_id] = {
"news_title": news_title,
"news_url": news_url,
"news_description": news_description,
"news_datetime": news_datetime
}
fresh_dictionary[news_id] = {
"news_title": news_title,
"news_url": news_url,
"news_description": news_description,
"news_datetime": news_datetime
}
time.sleep(1)
with open("news_dictionary.json", "w", encoding="UTF-8") as file:
json.dump(news_dict, file, indent=4, ensure_ascii=False)
return fresh_dictionary
def main():
# gethtmlandcontent()
newnews()
if __name__ == "__main__":
main()