- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从 Yahoo News
和“Bing News”页面抓取数据。我想要抓取的数据是标题或/和标题下方的文本(无论可以抓取什么)以及发布的日期(时间)。
我写了一段代码,但它没有返回任何内容。这是我的 url
的问题,因为我收到 response 404
你能帮我解决一下吗?
这是“Bing”的代码
from bs4 import BeautifulSoup
import requests
term = 'usa'
url = 'http://www.bing.com/news/q?s={}'.format(term)
response = requests.get(url)
print(response)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup)
这是针对雅虎
的:
term = 'usa'
url = 'http://news.search.yahoo.com/q?s={}'.format(term)
response = requests.get(url)
print(response)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup)
请帮我生成这些网址,它们背后的逻辑是什么,我还是个菜鸟:)
最佳答案
基本上你的网址是错误的。您必须使用的网址与使用常规浏览器时在地址栏中找到的网址相同。通常大多数搜索引擎和聚合器使用 q
参数作为搜索词。大多数其他参数通常不是必需的(有时它们是必需的 - 例如,用于指定结果页号等..)。
必应
from bs4 import BeautifulSoup
import requests
import re
term = 'usa'
url = 'https://www.bing.com/news/search?q={}'.format(term)
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
for news_card in soup.find_all('div', class_="news-card-body"):
title = news_card.find('a', class_="title").text
time = news_card.find(
'span',
attrs={'aria-label': re.compile(".*ago$")}
).text
print("{} ({})".format(title, time))
输出
Jason Mohammed blitzkrieg sinks USA (17h)
USA Swimming held not liable by California jury in sexual abuse case (1d)
United States 4-1 Canada: USA secure payback in Nations League (1d)
USA always plays the Dalai Lama card in dealing with China, says Chinese Professor (1d)
...
雅虎
from bs4 import BeautifulSoup
import requests
term = 'usa'
url = 'https://news.search.yahoo.com/search?q={}'.format(term)
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
for news_item in soup.find_all('div', class_='NewsArticle'):
title = news_item.find('h4').text
time = news_item.find('span', class_='fc-2nd').text
# Clean time text
time = time.replace('·', '').strip()
print("{} ({})".format(title, time))
输出
USA Baseball will return to Arizona for second Olympic qualifying chance (52 minutes ago)
Prized White Sox prospect Andrew Vaughn wraps up stint with USA Baseball (28 minutes ago)
Mexico defeats USA in extras for Olympic berth (13 hours ago)
...
关于python - 使用 Python 和 BeautifulSoup 生成雅虎新闻和 Bing 新闻的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58903413/
我最近开始接触网络编程,我完全不知所措。我已经开始学习基础知识、html、css 和 javascript。在我的第一个网页上,我有兴趣为我发布更新的地方实现一个下拉新闻部分。我能举出的最好的例子就是
我的 Wordpress 主题在我放大评论框时没有扩展其内容区域,因此整个内容都在页脚上。页脚保持固定在页面底部,但当我展开评论框时不会自行向下推... 我尝试阅读其他问题,但我没有解决那个问题。 代
我正在运行 TYPO3 V6 和最新版本的 tx_news(不是 tt_news),当我尝试更改 LIST 显示的排序顺序时,插件中的设置不会覆盖 Typoscript 设置。似乎没有办法更改 Lis
根据 Google 允许您的文章/新闻出现在 Google 新闻中: Display a three-digit number. The URL for each article must conta
我想问一下是否可以使用表单提交后发送到用户邮件的唯一代码创建注册,我不确定如何正确地做。 for example : The user enters his email and the system
我目前正在使用在 Google 新闻语料库上训练的 Word2Vec 模型(来自 here)由于这只针对 2013 年之前的新闻进行训练,因此我需要根据 2013 年之后的新闻更新向量并在词汇表中添加
所有的麻烦都开始了,我无法按画廊的右箭头(右箭头出现在图片中) 我看到只有当我将栏移到右侧时,我才能点击箭头。 如您所见,我在 Firefox 中打开了 F12,指向了网站的右侧部分。 我看到它是空的
我有一些代码运行良好,它只是添加了一个水平新闻提要(或我列出的任何信息)它运行良好,没有闪烁,但是当我向它添加更多数据时,它似乎需要一段时间才能加载并且速度变化?我还有很多信息要添加到其中,但我不想在
我有点坚持 RewriteRule 301,从旧新闻 ID 更改为新新闻 ID 这是我尝试过的: RewriteRule ^/news/0(.*)$ /news/$1 [L,R=301] 假设新闻 U
我对 Google 新闻 RSS 提要的使用有疑问。 Google 新闻帮助说明了这一点: Why Google might block an RSS feed In some cases, Goog
我想在我的网站上加入新闻,但主要主题必须是“书籍”或“作者”等。基本上,我需要选择我提到的这两个或作者姓名等自定义主题。 但我不知道怎么做,因为文档真的很差(或者我找不到)。 添加它的查询参数是什么?
最好的方法是什么? 我想解析新闻,然后使用关键字之类的内容过滤它们并找到匹配项。 有人已经这样做了吗?而且,这是合法的吗? 最佳答案 您可以使用 google 新闻网址 http://news.goo
我有一个获取热门新闻头条的 js 函数。它已按 promise 返回,但我无法访问这些文章。这是我的代码 function news09() { var url = 'https://ne
我想让我的 TableView 加载更多动画,例如 Facebook 新闻,并在底部显示动画指示器事件。 有什么指导可以帮助我做到这一点吗?谢谢。 最佳答案 有几种方法可以做到这一点 在最后一个 in
我正在为我的网站创建一个新闻系统。我有一个主页 {index.php}(显示所有文章)和一个文章页面 (article.php) 我遇到的问题是在文章页面上选择内容。 当你点击 index.php 上
我已成功调用新闻 API 并将结果放入 DataFrame,但仅限于第 1 页。 def get_articles(keyword): all_articles = newsapi.get_eve
我有一个适合我网站的新闻 slider ,我想使用它,但我遇到了一个小问题。完成 HTML 和 CSS 后,我需要旋转“展示柜”,现在我已经尝试使用 Jquery 的不同指令,但一点运气都没有。有人可
因此,我必须根据编写的 javascript 文件(如下)创建搜索查询,并且还必须使用此 URL 来创建搜索查询。在 URL 末尾,您可以添加任何您喜欢的搜索词。例如,我们将搜索食物:https://
我在 TYPO3 8.7.13 中使用来自 Georg Ringer 的新闻扩展。 如果没有选择图像进行预览,扩展程序会显示一个虚拟图像。是否可以改用文章中的第一张图片? 谢谢 最佳答案 当然,您需要
我是typo3 的新手,我需要有关新闻扩展和$TCA 配置的帮助。 我做了一个名为“Activité”的扩展,它从 News 扩展而来。这很顺利。我创建了一些自定义字段,并且能够从“常规”选项卡中已经
我是一名优秀的程序员,十分优秀!