Как я могу вычеркнуть все статьи из URL - PullRequest
0 голосов
/ 01 декабря 2018

Новичок в Python, и я пытаюсь очистить определенный веб-сайт, и у меня возникли проблемы.Я пытаюсь почистить статьи из "https://www.cnn.com/business"", но происходит то, что я зачищаю "cnn.com" и вместо этого собираю все эти статьи. Есть ли способ почистить только бизнес-раздел веб-сайта? Еслимой подход совершенно неправильный, я хотел бы знать, что было бы лучшим способом сделать это. Спасибо

У меня есть файл json, который имеет ссылку на cnn.com/business, и я использую газетубиблиотека на Python

#!pip install feedparser
#!pip install newspaper3k

import feedparser as fp
import numpy as np
import json
import newspaper
from newspaper import Article
from time import mktime
from datetime import datetime
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import csv

# Set the limit for number of articles to download
LIMIT = 10
articles_array = []

data = {}
data['newspapers'] = {}

# Loads the JSON files with news sites
with open('newspapers.json') as data_file:
    companies = json.load(data_file)


paper = newspaper.build(value['link'], memoize_articles=False)
newsPaper = {
    "link": value['link'],
    "articles": [],
}
noneTypeCount = 0
for content in paper.articles:
    if count > LIMIT:
        break
    try:
        content.download()
        content.parse()
    except Exception as e:
        print(e)
        print("continuing...")
        continue

article = {}
article['title'] = content.title
article['authors'] = content.authors
article['text'] = content.text
article['top_image'] =  content.top_image
article['link'] = content.url
article['published'] = content.publish_date
newsPaper['articles'].append(article)
articles_array.append(article)       
print(count, "articles downloaded from", company, " using newspaper, url: ", content.url)
count = count + 1
#noneTypeCount = 0
count = 1
data['newspapers'][company] = newsPaper

1 Ответ

0 голосов
/ 01 декабря 2018

Это скорее комментарий, а не полный пост.

    import urllib
    import re
    NUM_LINKS_YOU_WANT = 10
    urllib.request.urlretrieve("https://edition.cnn.com/business", ".temp_file")
    occurrences = []
    for line in open(".temp_file"):
        if "index.html" in line:
            occurrences.append(line)
    positions = [m.start() for m in re.finditer('index.html', occurrences[-1])]
    line = occurrences[-1]
    links = []
    for p in positions:
        href = line[0:p].rfind("href")
        links.append(" https://edition.cnn.com"+line[href+6:p])
    print(links[0:NUM_LINKS_YOU_WANT])
...