Python 用新闻纸刮网3K,只得到50篇文章
我想用报纸3K在一个法国网站上搜集数据,结果只有50篇文章。这个网站有50多篇文章。我错在哪里 我的目标是把这个网站上的所有文章都删掉 我试过这个:Python 用新闻纸刮网3K,只得到50篇文章,python,newspaper3k,Python,Newspaper3k,我想用报纸3K在一个法国网站上搜集数据,结果只有50篇文章。这个网站有50多篇文章。我错在哪里 我的目标是把这个网站上的所有文章都删掉 我试过这个: import newspaper legorafi_paper = newspaper.build('http://www.legorafi.fr/', memoize_articles=False) # Empty list to put all urls papers = [] for article in legorafi_paper.
import newspaper
legorafi_paper = newspaper.build('http://www.legorafi.fr/', memoize_articles=False)
# Empty list to put all urls
papers = []
for article in legorafi_paper.articles:
papers.append(article.url)
print(legorafi_paper.size())
这篇文章的结果是50篇文章
我不明白为什么新闻纸3K只会刮50篇文章,而不会更多
更新我尝试的内容:
def Foo(firstTime = []):
if firstTime == []:
WebDriverWait(driver, 30).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"div#appconsent>iframe")))
firstTime.append('Not Empty')
else:
print('Cookies already accepted')
%%time
categories = ['societe', 'politique']
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import newspaper
import requests
from newspaper.utils import BeautifulSoup
from newspaper import Article
categories = ['people', 'sports']
papers = []
driver = webdriver.Chrome(executable_path="/Users/name/Downloads/chromedriver 4")
driver.get('http://www.legorafi.fr/')
for category in categories:
url = 'http://www.legorafi.fr/category/' + category
#WebDriverWait(self.driver, 10)
driver.get(url)
Foo()
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button--filled>span.baseText"))).click()
pagesToGet = 2
title = []
content = []
for page in range(1, pagesToGet+1):
print('Processing page :', page)
#url = 'http://www.legorafi.fr/category/france/politique/page/'+str(page)
print(driver.current_url)
#print(url)
time.sleep(3)
raw_html = requests.get(url)
soup = BeautifulSoup(raw_html.text, 'html.parser')
for articles_tags in soup.findAll('div', {'class': 'articles'}):
for article_href in articles_tags.find_all('a', href=True):
if not str(article_href['href']).endswith('#commentaires'):
urls_set.add(article_href['href'])
papers.append(article_href['href'])
for url in papers:
article = Article(url)
article.download()
article.parse()
if article.title not in title:
title.append(article.title)
if article.text not in content:
content.append(article.text)
#print(article.title,article.text)
time.sleep(5)
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
driver.find_element_by_xpath("//a[contains(text(),'Suivant')]").click()
time.sleep(10)
更新09-21-2020
我重新检查了您的代码,它工作正常,因为它正在提取的主页上的所有文章。本页面上的文章是分类页面的亮点,如societe、sports等
下面的示例来自主页的源代码。这些文章中的每一篇都列在体育类网页上
<div class="cat sports">
<a href="http://www.legorafi.fr/category/sports/">
<h4>Sports</h4>
<ul>
<li>
<a href="http://www.legorafi.fr/2020/07/24/chaque-annee-25-des-lutteurs-doivent-etre-operes-pour-defaire-les-noeuds-avec-leur-bras/" title="Voir l'article 'Chaque année, 25% des lutteurs doivent être opérés pour défaire les nœuds avec leur bras'">
Chaque année, 25% des lutteurs doivent être opérés pour défaire les nœuds avec leur bras</a>
</li>
<li>
<a href="http://www.legorafi.fr/2020/07/09/frank-mccourt-lom-nest-pas-a-vendre-sauf-contre-beaucoup-dargent/" title="Voir l'article 'Frank McCourt « L'OM n'est pas à vendre sauf contre beaucoup d'argent »'">
Frank McCourt « L'OM n'est pas à vendre sauf contre beaucoup d'argent </a>
</li>
<li>
<a href="http://www.legorafi.fr/2020/06/10/euphorique-un-parieur-appelle-son-fils-betclic/" title="Voir l'article 'Euphorique, un parieur appelle son fils Betclic'">
Euphorique, un parieur appelle son fils Betclic </a>
</li>
</ul>
<img src="http://www.legorafi.fr/wp-content/uploads/2015/08/rubrique_sport1-300x165.jpg"></a>
</div>
</div>
如果我将上面代码中的URL更改为:,它将返回与相同数量的文章。在查看了Paper on的源代码之后,模块似乎正在使用,它似乎正在使用urlparse的netloc段。netloc是。我注意到,这是一个已知的问题与报纸的基础上,这个开放的
要获得所有文章,它变得更加复杂,因为您必须使用一些附加模块,包括requests和BeautifulSoup。这个
后者可以从报纸上称为。可以使用requests和BeautifulSoup对下面的代码进行优化,以获得主页和分类页面上源代码中的所有文章
如果您需要获取分类页面子页面中列出的文章(politique目前有120个子页面),那么您必须使用Selenium之类的工具来单击链接
希望此代码能帮助您更接近实现目标。
Stackoverflow
有一种特殊的方法(和快捷键)来格式化多行代码。经过一些计算后,网站可能会阻止您,您可以联系他们的网站管理员,他们可以提供比您所能获得的更好的收藏,如果你在做一些科学或学习,他们可能会从中受益,也许是免费的!谢谢,@ti7您知道我是否可以用python代码绕过它吗?@LJRB您可以使用,它将允许您的程序充当多个独立客户端,而不是单个客户端。然而,要求更好地直接访问数据(可能像一个没有50页限制的帐户一样简单),并在您正在制作的作品中引用这些数据,这可能是他们要求您获得更高质量访问的全部要求(他们知道任何人都可以编写程序来阅读他们的网站,如果你有一个网站,你会看到许多机器人正在积极阅读你的网站)。谢谢您的帮助@Life是复杂的,我尝试了您的代码,但每个类别都有相同的53个URL。我能做什么?嗯…LMK看看这个。@LJRB请在您的问题中提供一些详细信息,说明您想从网站或其子页面中刮取哪些文章。一旦您这样做,我可以排除我的答案。您好,我想继续如果可能的话,强奸所有类别和所有页面上的所有文章。@LJRB看看这些问题是否有用-
import newspaper
legorafi_paper = newspaper.build('http://www.legorafi.fr', memoize_articles=False)
papers = []
urls_set = set()
for article in legorafi_paper.articles:
# check to see if the article url is not within the urls_set
if article.url not in urls_set:
# add the unique article url to the set
urls_set.add(article.url)
# remove all links for article commentaires
if not str(article.url).endswith('#commentaires'):
papers.append(article.url)
print(len(papers))
# output
35
import newspaper
import requests
from newspaper.utils import BeautifulSoup
papers = []
urls_set = set()
legorafi_paper = newspaper.build('http://www.legorafi.fr',
fetch_images=False, memoize_articles=False)
for article in legorafi_paper.articles:
if article.url not in urls_set:
urls_set.add(article.url)
if not str(article.url).endswith('#commentaires'):
papers.append(article.url)
legorafi_urls = {'monde-libre': 'http://www.legorafi.fr/category/monde-libre',
'politique': 'http://www.legorafi.fr/category/france/politique',
'societe': 'http://www.legorafi.fr/category/france/societe',
'economie': 'http://www.legorafi.fr/category/france/economie',
'culture': 'http://www.legorafi.fr/category/culture',
'people': 'http://www.legorafi.fr/category/people',
'sports': 'http://www.legorafi.fr/category/sports',
'hi-tech': 'http://www.legorafi.fr/category/hi-tech',
'sciences': 'http://www.legorafi.fr/category/sciences',
'ledito': 'http://www.legorafi.fr/category/ledito/'
}
for category, url in legorafi_urls.items():
raw_html = requests.get(url)
soup = BeautifulSoup(raw_html.text, 'html.parser')
for articles_tags in soup.findAll('div', {'class': 'articles'}):
for article_href in articles_tags.find_all('a', href=True):
if not str(article_href['href']).endswith('#commentaires'):
urls_set.add(article_href['href'])
papers.append(article_href['href'])
print(len(papers))
# output
155