【问题标题】:How I get urls in website that content Apple article only (in python)?如何在仅包含 Apple 文章的网站中获取网址(在 python 中)?
【发布时间】:2018-01-23 03:51:31
【问题描述】:

我是 Python 新手。我想提取谈论苹果的新闻文章。我的项目想从 BBC 网站获取仅关于苹果文章的文章。我的代码如下我抓取网站。但我无法确定如何我只收到 Apple 文章。任何人都可以帮助解决我的问题。

代码

from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
#pass the URL
url = urlopen("http://www.bbc.com")
#read the source from the URL
readHtml = url.read()
#close the url
url.close()
#passing HTML to scrap it
soup = BeautifulSoup(readHtml, 'html.parser')
all_tag_a = soup.find_all("a", limit=10)
for links in all_tag_a:
#just pull the href part from each link
 print(links.get('href'))

【问题讨论】:

  • 您可能会使用以下 api 或类似的东西来获取与特定关键字(如 Apple)相关的文章,而不是尝试自己执行请求。 newsapi.org/s/bbc-news-api
  • @SuryaAvala 谢谢..我想抓取给定时间段的文章。你能进一步解释一下。我如何改变我的逻辑。
  • @SuryaAvala 我在这段代码中添加了import requests url = ('https://newsapi.org/v2/everything?' 'q=Apple&' 'from=2018-01-23&' 'sortBy=popularity&' 'apiKey=42fd08167d994786b197aa193e26f954') response = requests.get(url) print( r.json) r 是什么。你能解释一下吗。我收到类似** print (r.json) NameError: name 'r' is not defined**

标签: python events


【解决方案1】:

请尝试以下操作:
from urllib.parse import urlparse
o = urlparse('https://www.apple.com/in/') #URL的形式是-> scheme://netloc/path;parameters?query#fragment.
#whatever URLs you are geeting write it in above statement,可能是一个循环会有所帮助

如果 o.netloc 中的“苹果”:
#如果找到匹配项,那么这里应该是您的 Apple 网址
      print o.geturl()
Please refer this for more onformtaion

【讨论】:

  • 非常感谢您的帮助。您的代码很有帮助,但不是我的预期输出。我想获取有关 Apple 的网址。作为 BBC 网站的示例,不同部分中有很多文章。所以我想要获取有关 Apple 的网址。所以你能帮帮我吗。非常感谢
  • 那我觉得你应该参考这个问题link
  • 感谢您的支持。该链接对我有帮助。link
猜你喜欢
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多