Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python过滤器抓取动态网站_Python_Web Scraping_Dynamic - Fatal编程技术网

用python过滤器抓取动态网站

用python过滤器抓取动态网站,python,web-scraping,dynamic,Python,Web Scraping,Dynamic,我想在网站中刮取一个表:。问题是,为了获得所需的数据,需要使用过滤器,否则您只能在进入网站后获得呈现的数据!请帮忙 这是我用来刮表的代码: import requests import bs4 import pandas as pd URL = 'https://www.feedtables.com/fr/content/table-dry-matter' page = requests.get(URL) soup = bs4.BeautifulSoup(page.text,'lxml')

我想在网站中刮取一个表:。问题是,为了获得所需的数据,需要使用过滤器,否则您只能在进入网站后获得呈现的数据!请帮忙

这是我用来刮表的代码:

import requests
import bs4 
import pandas as pd

URL = 'https://www.feedtables.com/fr/content/table-dry-matter'
page = requests.get(URL)

soup = bs4.BeautifulSoup(page.text,'lxml')
cases = soup.find('table', class_= 'views-table sticky-enabled cols-16')
headers=[]
for i in cases.find_all('th'):
    headers.append(i.text.strip())

Data=[]
for i in cases.find_all('td'):
    Data.append(i.text.strip())

chunks = [Data[x:x+16] for x in range(0, len(Data), 16)]
chunks


你不能用bs4处理它。但是你可以很容易地使用硒

#Download chromedriver from https://chromedriver.chromium.org/downloads  
#pip install selenium


    import time, os
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    chromedriver = "C:\\Program Files\Google\Chrome\Application\chromedriver"
    os.environ["webdriver.chrome.driver"] = chromedriver

    url_head = 'https://www.google.com/search?q="HOW+TO+USE+SELENIUM"'
    driver = webdriver.Chrome(chromedriver)
    driver.get(url)
这就是你可以连接的方式。然后你可以像这样点击

driver.find_element_by_xpath('//html/body/div[1]/div/ul/li[3]').click()
有关更多信息,请检查:

有不同的方法
  • 正如burak提到的自动化

  • 使用您的
    请求
    并将
    参数
    添加到您的url中,因为有
    提要猫
    参数猫
    ,您可以从表上方的
    选择
    更新-查看编辑部分

  • 我的示例循环浏览
    feed\u cat
    selectbox by的选项

    soup.find('select',attrs={'name':'feed_cat'})。find_all('option')[1:3]
    (只需第二个和第三个选项即可演示)

    然后它
    用这些参数请求

    page=requests.get(url+'?提要\'cat='+选项['value']+'&参数\'cat=All',headers=headers)

    最后,您可以循环数据以生成数据帧

    注意:在一个请求中获取所有数据的简单方法
    https://www.feedtables.com/fr/content/table-dry-matter?feed_cat=All¶meter_cat=All
    无法工作,您将收到服务器错误

    请求示例

    import bs4
    import requests
    import pandas as pd
    
    url = "https://www.feedtables.com/fr/content/table-dry-matter"
    headers = {"user-agent": "Mozilla/5.0"}
    
    page = requests.get(url, headers=headers)
    soup = bs4.BeautifulSoup(page.text,'lxml')
    
    data = []
    
    for option in soup.find('select',attrs={'name': 'feed_cat'}).find_all('option')[1:3]:
        page = requests.get(url+'?feed_cat='+option['value']+'&parameter_cat=All', headers=headers)
        soup = bs4.BeautifulSoup(page.text,'lxml')
        
        cases = soup.select_one('table.views-table.sticky-enabled')
        
        th=[]
        td=[]
        for i in cases.find_all('th'):
            th.append(i.text.strip())
            
        for row in cases.find_all('tr')[1::2]:
            td.append([i.get_text(strip=True) for i in row.find_all('td')])   
                
        
        data.append({'cat' : option.text, 'headers':th, 'data':td})
    
    df=pd.DataFrame(data[0]['data'], columns=data[0]['headers'])
    df
    
    输出(执行示例看起来更好;)

    编辑 使用
    pd.read\u html()


    非常感谢你,伯拉克先生。这意味着我应该在这类网站上使用selenium!再次感谢您的回复!哇!伙计,这就是为什么我喜欢编程社区!太爱你了,兄弟!你太棒了!谢谢!干得好,亲爱的!再次感谢您的编辑!
    Matière première    MS %    MAT %   CB %    MGr %   MM %    MMins % NDF %   ADF %   Lignine %   ... GluDIS volaille g/kg    GlyDIS volaille g/kg    SerDIS volaille g/kg    ProDIS volaille g/kg    CED MJ/kg   ConsP g P/kg    CC g CO2eq/kg   Acid. mol H+eq/kg   Eutrophisation g PO4---eq/kg    OS m²yr/kg
    0   Avoine  100 10.8    13.1    5.4 2.9 1.2 35.7    16.3    2.6 ... 15  4.2 4   4.9 3.02›››3.23›››  3.32›››3.32›››  555›››566›››    0.0139›››0.014›››   61.23›››61.52›››    0.0002›››0.0002›››
    1   Avoine décortiquée  100 12.8    4.5 3.1 2.5 1.2 12.8    5.3 1.9 ... 19.9    5.2 5.1 6.3                     
    2   Avoine floconnée    100 10.8    13.1    5.4 2.9 1.2 35.7    16.3    2.6 ... 15  4.2 4   4.9                     
    3   Blé dur 100 16.4    3   2   2.1 0.05    15.9    4.2 1.3 ... 48.2    5.5 7.2 16.3    
    
    import bs4
    import requests
    import pandas as pd
    
    url = "https://www.feedtables.com/fr/content/table-dry-matter"
    headers = {"user-agent": "Mozilla/5.0"}
    
    page = requests.get(url, headers=headers)
    soup = bs4.BeautifulSoup(page.text,'lxml')
    
    df_list = []
    
    for url in [url+'?feed_cat='+option['value']+'&parameter_cat=All' for option in soup.find('select',attrs={'name': 'feed_cat'}).find_all('option')][1:3]:
        df_list.append(pd.read_html(url)[0])
    
    df = df_list[0].dropna(how='all')
    df