【问题标题】:Web scraping url not changing while search [closed]搜索时网页抓取网址不变[关闭]
【发布时间】:2019-07-04 15:50:42
【问题描述】:

我正在尝试抓取 https://in.udacity.com/courses/all。我需要在输入搜索查询时显示课程。例如:如果我输入 python,有 17 门课程作为结果。我只需要获取这些课程。这里搜索查询没有作为 url 的一部分传递。(不是 get 方法)。所以 html 内容也没有改变。那么如何在不浏览整个课程列表的情况下获取这些结果。 在这段代码中,我正在获取所有课程链接以获取其内容并在该内容中搜索搜索词。但它并没有给我预期的结果。

import requests
from bs4 import BeautifulSoup
from bs4 import BeautifulSoup
from bs4.element import Comment
import urllib.request
from urllib.request import Request, urlopen

def tag_visible(element):
    if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
        return False
    if isinstance(element, Comment):
        return False
    return True


def text_from_html(body):
    soup = BeautifulSoup(body, 'html.parser')
    texts = soup.findAll(text=True)
    visible_texts = filter(tag_visible, texts)  
    return u" ".join(t.strip() for t in visible_texts)

page = requests.get("https://in.udacity.com/courses/all")
soup = BeautifulSoup(page.content, 'lxml')
courses = soup.select('a.capitalize')

search_term = input("enter the course:")
for link in courses:
    #print("https://in.udacity.com" + link['href'])
    html = urllib.request.urlopen("https://in.udacity.com" + link['href']).read()

    if search_term in text_from_html(html).lower():
        print('\n'+link.text)
        print("https://in.udacity.com" + link['href'])

【问题讨论】:

标签: python web-scraping beautifulsoup


【解决方案1】:

使用requestsBeautifulSoup

import requests
from bs4 import BeautifulSoup

page = requests.get("https://in.udacity.com/courses/all")
soup = BeautifulSoup(page.content, 'html.parser')
courses = soup.find_all("a", class_="capitalize")

for course in courses:
    print(course.text)

输出:

VR Foundations
VR Mobile 360
VR High-Immersion
Google Analytics
Artificial Intelligence for Trading
Python Foundation
.
.
.

编辑:

正如@Martin Evans 所解释的,搜索背后的 Ajax 调用并没有按照您的想法进行,它可能会保持搜索的计数,即 有多少用户搜索了 AI 它基本上正在根据search_term中的关键字过滤掉搜索:

import requests
from bs4 import BeautifulSoup
import re

page = requests.get("https://in.udacity.com/courses/all")
soup = BeautifulSoup(page.content, 'html.parser')
courses = soup.find_all("a", class_="capitalize")
search_term = "AI"

for course in courses:
    if re.search(search_term, course.text, re.IGNORECASE):
        print(course.text)

输出:

AI Programming with Python
Blockchain Developer Nanodegree program
Knowledge-Based AI: Cognitive Systems

【讨论】:

  • @Merin 你为什么取消我的回答?
  • 我尝试了您建议的方法。(通过过滤掉所有包含搜索词的课程)。但它给我的课程比网页中列出的课程更多。例如,当我搜索python 17 课程即将出现在网页中。但是当我过滤掉时。超过 17 门课程即将到来。
【解决方案2】:

udacity 页面实际上会在您请求时返回所有可用的课程。当您输入搜索时,该页面只是过滤可用数据。这就是为什么您在输入搜索时看不到 URL 的任何更改的原因。使用浏览器的开发工具进行检查也证实了这一点。这也解释了为什么“搜索”如此之快。

因此,如果您正在搜索给定的课程,您只需要自己过滤结果。例如:

import requests
from bs4 import BeautifulSoup

req = requests.get("https://in.udacity.com/courses/all")
soup = BeautifulSoup(req.content, "html.parser")
a_tags = soup.find_all("a", class_="capitalize")

print("Number of courses:", len(a_tags))
print()

for a_tag in a_tags:
    course = a_tag.text

    if "python" in course.lower():
        print(course)

这将显示标题中带有Python 的所有课程:

Number of courses: 225

Python Foundation
AI Programming with Python
Programming Foundations with Python
Data Structures & Algorithms in Python

【讨论】:

    【解决方案3】:

    阅读教程,了解如何使用requests(用于发出 HTTP 请求)和BeautifulSoup(用于处理 HTML)。这将教您下载页面和从 HTML 中提取数据所需了解的知识。

    您将使用函数BeautifulSoup.find_all() 来定位页面HTML 中的所有<div> 元素以及class=course-summary-card。你想要的内容在那个<div> 中,在阅读了上面的链接之后,你应该很容易弄清楚其余的内容;)

    顺便说一句,在您学习如何执行此操作时,一个对您有用的工具是使用“检查元素”功能(适用于 Chrome/Firefox),可以通过右键单击浏览器中的元素来访问该功能,使您能够查看您有兴趣提取的元素周围的源代码,这样您就可以获得类或 id、父 div 等信息,这些信息将允许您在 BeautifulSoup/lxml/etc 中选择它。

    【讨论】:

    • 我觉得除了美汤和要求提取搜索结果之外应该有更好的选择
    • 我想如果你阅读更多关于网页抓取的内容,你会发现这可能是解决你的问题的最简单的方法。您可能希望有一些更简单的东西,但这与编写脚本以获取 HTML 并从中提取文本一样简单。
    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 2013-06-17
    相关资源
    最近更新 更多