| import requests, sys, json, lzma, os, time
|
| from bs4 import BeautifulSoup
|
| from multiprocessing.pool import Pool
|
|
|
|
|
| stories = []
|
| with lzma.open("0-stories.jsonl.xz", "rt") as fin:
|
| for idx, line in enumerate(fin):
|
| data = json.loads(line)
|
|
|
| data["_id"] = idx
|
| stories.append(data)
|
| print(len(stories), "stories.")
|
|
|
| def crawl(story):
|
| story_id = story["_id"]
|
| story_url = story["url"]
|
|
|
| r = requests.get(story_url)
|
| soup = BeautifulSoup(r.content, "html.parser")
|
|
|
| chapters = []
|
| for a in soup.select("#tab-chapper li a"):
|
| title = a.text.strip()
|
| url = a["href"]
|
| chapters.append({"title": title, "url": url})
|
|
|
| story["html"] = str(soup)
|
| story["chapters"] = chapters
|
| return story
|
|
|
|
|
|
|
|
|
| count = 0
|
| with Pool() as pool:
|
| for story in pool.imap_unordered(crawl, stories):
|
| with lzma.open("1.jsonl.xz", "at") as fout:
|
| fout.write(json.dumps(story, ensure_ascii=False) + "\n")
|
| count += 1
|
| print(count, story["url"], len(story["chapters"]))
|
|
|