Python 从Web中提取数据

Python 从Web中提取数据,python,web-scraping,beautifulsoup,collect,Python,Web Scraping,Beautifulsoup,Collect,一个非常新的问题。 我正在为我的家庭使用编写一个小型python脚本,它将收集特定机票的数据 我想从skyscanner中提取数据(使用BeautifulSoap和urllib)。例如: 我对存储在这种元素中的所有数据感兴趣,特别是价格: 因为它们不在HTML中,我可以提取它们吗?我认为问题在于,这些值是通过浏览器运行的javascript代码呈现的,而urllib则不是-您应该使用可以执行javascript代码的库 我只是在谷歌上搜索了crawler-python-javascript,得

一个非常新的问题。 我正在为我的家庭使用编写一个小型python脚本,它将收集特定机票的数据

我想从skyscanner中提取数据(使用BeautifulSoap和urllib)。例如:

我对存储在这种元素中的所有数据感兴趣,特别是价格:


因为它们不在HTML中,我可以提取它们吗?

我认为问题在于,这些值是通过浏览器运行的javascript代码呈现的,而
urllib
则不是-您应该使用可以执行javascript代码的库

我只是在谷歌上搜索了
crawler-python-javascript
,得到了一些建议使用or的stackoverflow问题和答案。您可以通过使用这些库。以下是两个片段:


我认为问题在于,这些值是通过javascript代码呈现的,而您的浏览器运行而
urllib
不运行—您应该使用可以执行javascript代码的库

我只是在谷歌上搜索了
crawler-python-javascript
,得到了一些建议使用or的stackoverflow问题和答案。您可以通过使用这些库。以下是两个片段:


我一直在研究同样的问题。我被介绍给了Beautifulsoup,后来又了解了Scrapy。Beautifulsoup非常容易使用,特别是如果你是新手的话。Scrapy显然有更多的“功能”,但我相信你可以用Beautifulsoup满足你的需求

对于无法访问通过Javascript加载信息的网站,我也有同样的问题,谢天谢地,Selenium是救世主

我们可以找到一个关于硒的介绍

安装:
pip安装selenium

下面是我编写的一个简单的类。您可以将其另存为.py文件并将其导入到项目中。如果调用方法
retrieve\u source\u code(self,domain)
并发送您试图解析的超链接,它将返回完全加载页面的源代码,然后您可以将其放入Beautifulsoup并找到您要查找的信息

例:

现在,您可以像通常使用Beautifulsoup一样解析
soup

我希望这对你有帮助

from selenium import webdriver
import requests

class SeleniumWebScraper():

    def __init__(self):
        self.source_code = ''
        self.is_page_loaded = 0
        self.driver = webdriver.Firefox()
        self.is_browser_closed = 0
        # To ensure the page has fully loaded we will 'implicitly' wait 
        self.driver.implicitly_wait(10)  # Seconds

    def close(self):
        self.driver.close()
        self.clear_source_code()
        self.is_page_loaded = 0
        self.is_browser_closed = 1

    def clear_source_code(self):
        self.source_code = ''
        self.is_page_loaded = 0

    def retrieve_source_code(self, domain):
        if self.is_browser_closed:
            self.driver = webdriver.Firefox()
        # The driver.get method will navigate to a page given by the URL.
        #  WebDriver will wait until the page has fully loaded (that is, the "onload" event has fired)
        #  before returning control to your test or script.
        # It's worth nothing that if your page uses a lot of AJAX on load then
        #  WebDriver may not know when it has completely loaded.
        self.driver.get(domain)

        self.is_page_loaded = 1
        self.source_code = self.driver.page_source
        return self.source_code

我一直在研究同样的问题。我被介绍给了Beautifulsoup,后来又了解了Scrapy。Beautifulsoup非常容易使用,特别是如果你是新手的话。Scrapy显然有更多的“功能”,但我相信你可以用Beautifulsoup满足你的需求

对于无法访问通过Javascript加载信息的网站,我也有同样的问题,谢天谢地,Selenium是救世主

我们可以找到一个关于硒的介绍

安装:
pip安装selenium

下面是我编写的一个简单的类。您可以将其另存为.py文件并将其导入到项目中。如果调用方法
retrieve\u source\u code(self,domain)
并发送您试图解析的超链接,它将返回完全加载页面的源代码,然后您可以将其放入Beautifulsoup并找到您要查找的信息

例:

现在,您可以像通常使用Beautifulsoup一样解析
soup

我希望这对你有帮助

from selenium import webdriver
import requests

class SeleniumWebScraper():

    def __init__(self):
        self.source_code = ''
        self.is_page_loaded = 0
        self.driver = webdriver.Firefox()
        self.is_browser_closed = 0
        # To ensure the page has fully loaded we will 'implicitly' wait 
        self.driver.implicitly_wait(10)  # Seconds

    def close(self):
        self.driver.close()
        self.clear_source_code()
        self.is_page_loaded = 0
        self.is_browser_closed = 1

    def clear_source_code(self):
        self.source_code = ''
        self.is_page_loaded = 0

    def retrieve_source_code(self, domain):
        if self.is_browser_closed:
            self.driver = webdriver.Firefox()
        # The driver.get method will navigate to a page given by the URL.
        #  WebDriver will wait until the page has fully loaded (that is, the "onload" event has fired)
        #  before returning control to your test or script.
        # It's worth nothing that if your page uses a lot of AJAX on load then
        #  WebDriver may not know when it has completely loaded.
        self.driver.get(domain)

        self.is_page_loaded = 1
        self.source_code = self.driver.page_source
        return self.source_code

您甚至不需要BeautifulSoup来提取数据

只要这样做,您的响应就会转换成一个非常容易处理的字典

text=json.loads(“您需要主响应内容的文本”)

现在可以打印字典中的任何键值对。
试试看。这非常简单。

您甚至不需要BeautifulSoup来提取数据

只要这样做,您的响应就会转换成一个非常容易处理的字典

text=json.loads(“您需要主响应内容的文本”)

现在可以打印字典中的任何键值对。
试试看。这非常简单。

可能重复的:,所以底线是使用可能重复的:,所以底线是使用我检查了一些其他论坛,找到了一个更适合我知识水平的解决方案(使用Webkit和QT)。如果有人感兴趣:我查看了一些其他论坛,找到了一个更适合我知识水平的解决方案(使用Webkit和QT)。如果有人感兴趣: