-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08c4765
commit f31ea58
Showing
3 changed files
with
92 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
from docx import Document | ||
from docx import Pt | ||
import re | ||
|
||
|
||
# Открываем файл .txt и читаем его содержимое | ||
with open('/mnt/d/ProjectsGo/WebScraper/documents/article.txt', 'r', encoding='utf-8') as txt_file: | ||
txt_content = txt_file.read() | ||
|
||
# Убираем лишние пробелы между строками (3 и более заменяем на 2) | ||
txt_content = re.sub(r'(\n\s{3,})', '\n\n ', txt_content) | ||
|
||
# Создаем новый документ .docx и добавляем текст | ||
doc = Document() | ||
first_paragraph = doc.add_paragraph(txt_content.split('\n', 1)[0]) | ||
run = first_paragraph.runs[0] | ||
run.font.size = Pt(18) # 1.5rem | ||
run.font.name = 'Fira Sans' | ||
|
||
# Добавляем оставшийся текст как обычно | ||
doc.add_paragraph(txt_content[len(first_paragraph.text) + 1:]) | ||
|
||
|
||
# Сохраняем документ в файл .docx | ||
doc.save(r'/mnt/d/ProjectsGo/WebScraper/documents/output.docx') | ||
from docx import Document | ||
from docx.shared import Pt | ||
import re | ||
|
||
|
||
# Открываем файл .txt и читаем его содержимое | ||
with open('/mnt/d/ProjectsGo/WebScraper/documents/article.txt', 'r', encoding='utf-8') as txt_file: | ||
txt_content = txt_file.read() | ||
|
||
# Убираем лишние пробелы между строками (3 и более заменяем на 2) | ||
txt_content = re.sub(r'(\n\s{3,})', '\n\n ', txt_content) | ||
|
||
# Создаем новый документ .docx и добавляем текст | ||
doc = Document() | ||
first_paragraph = doc.add_paragraph(txt_content.split('\n', 1)[0]) | ||
run = first_paragraph.runs[0] | ||
run.font.size = Pt(18) # 1.5rem | ||
run.font.name = 'Fira Sans' | ||
|
||
# Добавляем оставшийся текст как обычно | ||
doc.add_paragraph(txt_content[len(first_paragraph.text) + 1:]) | ||
|
||
|
||
# Сохраняем документ в файл .docx | ||
doc.save(r'/mnt/d/ProjectsGo/WebScraper/documents/output.docx') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/gocolly/colly" | ||
) | ||
|
||
func main() { | ||
var URL string | ||
fmt.Print("Введите URL: ") | ||
fmt.Scanf("%s", &URL) | ||
|
||
c := colly.NewCollector( | ||
colly.AllowedDomains("habr.com"), | ||
) | ||
|
||
var headerText string | ||
var articleText string | ||
|
||
c.OnRequest(func(r *colly.Request) { | ||
r.Headers.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36") | ||
// fmt.Printf("Посещение - %s\n\n", r.URL) | ||
}) | ||
|
||
c.OnHTML(".tm-title.tm-title_h1", func(h *colly.HTMLElement) { | ||
headerText = h.Text | ||
}) | ||
|
||
c.OnHTML(".tm-article-body", func(h *colly.HTMLElement) { | ||
articleText = h.Text | ||
}) | ||
|
||
c.OnScraped(func(r *colly.Response) { | ||
if headerText != "" && articleText != "" { | ||
fmt.Print("Вы уверены, что хотите перезаписать файлы? (yes/no): ") | ||
reader := bufio.NewReader(os.Stdin) | ||
answer, _ := reader.ReadString('\n') | ||
answer = answer[:len(answer)-1] | ||
|
||
if answer == "yes" { | ||
saveToTextFile(headerText, articleText) | ||
fmt.Println("Данные успешно сохранены в .docx файл.") | ||
} else { | ||
fmt.Println("Операция отменена.") | ||
} | ||
} else { | ||
fmt.Println("He удалось извлечь необходимые данные.") | ||
} | ||
}) | ||
|
||
c.Visit(URL) | ||
} | ||
|
||
func saveToTextFile(header, article string) { | ||
file, err := os.Create("/mnt/d/ProjectsGo/WebScraper/documents/article.txt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
|
||
file.WriteString(header + "\n\n\n") | ||
file.WriteString(article) | ||
} | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/gocolly/colly" | ||
) | ||
|
||
func main() { | ||
var URL string | ||
fmt.Print("Введите URL: ") | ||
fmt.Scanf("%s", &URL) | ||
|
||
c := colly.NewCollector( | ||
colly.AllowedDomains("habr.com"), | ||
) | ||
|
||
var headerText string | ||
var articleText string | ||
|
||
c.OnRequest(func(r *colly.Request) { | ||
r.Headers.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36") | ||
// fmt.Printf("Посещение - %s\n\n", r.URL) | ||
}) | ||
|
||
c.OnHTML(".tm-title.tm-title_h1", func(h *colly.HTMLElement) { | ||
headerText = h.Text | ||
}) | ||
|
||
c.OnHTML(".tm-article-body", func(h *colly.HTMLElement) { | ||
articleText = h.Text | ||
}) | ||
|
||
c.OnScraped(func(r *colly.Response) { | ||
if headerText != "" && articleText != "" { | ||
fmt.Print("Вы уверены, что хотите перезаписать файлы? (yes/no): ") | ||
reader := bufio.NewReader(os.Stdin) | ||
answer, _ := reader.ReadString('\n') | ||
answer = answer[:len(answer)-1] | ||
|
||
if answer == "yes" { | ||
saveToTextFile(headerText, articleText) | ||
fmt.Println("Данные успешно сохранены в .docx файл.") | ||
} else { | ||
fmt.Println("Операция отменена.") | ||
} | ||
} else { | ||
fmt.Println("He удалось извлечь необходимые данные.") | ||
} | ||
}) | ||
|
||
c.Visit(URL) | ||
} | ||
|
||
func saveToTextFile(header, article string) { | ||
file, err := os.Create("/mnt/d/ProjectsGo/WebScraper/documents/article.txt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
|
||
file.WriteString(header + "\n\n\n") | ||
file.WriteString(article) | ||
} |
Binary file not shown.