the below is the part of a scraped .html web novel chapter that i want to extract: <span…
- User
the below is the part of a scraped .html web novel chapter that i want to extract:
Chương 314: Thiên Thủy đi Toái Nham Quyền, Võ Thánh kỹ năng rửa mặt! Bát cấp bên trong luân đao! « 3 ». ... Tải ebook
your task is write a python script that scrape multiple chapters from a list of chapter urls from a .txt file then combine it into a single .txt file. separate each line by a blank line. example of the result format that i wanted (ignore the text inside the <, > and the ... marks when converting the chapters.) : Chương 314: Thiên Thủy đi Toái Nham Quyền, Võ Thánh kỹ năng rửa mặt! Bát cấp bên trong luân đao! « 3 ».
Mộng cảnh không gian.
Lục Thánh bước chậm ở đổ nát trên đường dài.
Trong tay hắn dẫn theo phía trước từ sát sinh Võ Thánh Cố Huyền trong tay kế thừa xuống chuôi này hắc sắc trường đao, chứng kiến có Zombie xuất hiện, lại thuận tay chém ra một đao.
...
Lục Thánh thần sắc khẽ động, nhịn không được mở miệng nói: "Toàn bộ quân khu diễn võ cụ thể khi nào thì bắt đầu ? Ta có thể không thể tự chính mình đi? ."
- Assistant
Below is a Python script that can be used to scrape multiple web novel chapters from a list of URLs stored in a
.txtfile and save the extracted content into a single.txtfile. The script uses requests to fetch HTML content and BeautifulSoup to parse the HTML and extract relevant text.pythonimport requests from bs4 import BeautifulSoup def extract_chapter_content(url): response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') # Extract chapter title title_tag = soup.select_one('.chapter-text') chapter_title = title_tag.get_text(strip=True).replace('<', '').replace('>', '') # Extract main text body chapter_body = soup.select_one('#chapter-c') paragraphs = chapter_body.find_all('p') # Compile text with blank line separation content = [chapter_title] + [p.get_text(strip=True) for p in paragraphs] formatted_chapter = '\n\n'.join(content) return f"<start of chapter>\n{formatted_chapter}\n<end of chapter>" def read_urls(filename): with open(filename, 'r') as file: return [line.strip() for line in file.readlines() if line.strip()] def write_to_file(chapters, output_filename): with open(output_filename, 'w', encoding='utf-8') as file: for chapter in chapters: file.write(chapter + '\n\n') def main(): input_filename = 'chapter_urls.txt' output_filename = 'combined_chapters.txt' urls = read_urls(input_filename) chapters = [extract_chapter_content(url) for url in urls] write_to_file(chapters, output_filename) print(f"Chapters have been successfully combined into {output_filename}") if __name__ == "__main__": main()Instructions:
- Save the list of URLs, each on a new line, in a file named
chapter_urls.txt. - Run the script. It will create a file called
combined_chapters.txtcontaining the combined chapter content. - Modify the script if necessary to locate the specific HTML elements based on the structure of the web page being scraped.
Dependencies:
- Install BeautifulSoup and requests by running:
code
pip install beautifulsoup4 requests
Note:
- Ensure that HTML selectors such as
'.chapter-text'and'#chapter-c'match the elements in the actual page structure you are scraping. - Be aware of the terms of service of the website you are scraping to ensure compliance with their data usage policies.
- Save the list of URLs, each on a new line, in a file named