Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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 使用Pandas,在正确选择网站下拉列表中的值后,如何获取从网站导入的数据帧?_Python_Python 2.7_Pandas_Dataframe - Fatal编程技术网

Python 使用Pandas,在正确选择网站下拉列表中的值后,如何获取从网站导入的数据帧?

Python 使用Pandas,在正确选择网站下拉列表中的值后,如何获取从网站导入的数据帧?,python,python-2.7,pandas,dataframe,Python,Python 2.7,Pandas,Dataframe,默认情况下,在网站“”中,将显示两个下拉列表中“跨流派”和“前10名广告商”值的数据表 参考图片: 我必须得到带有“运动”和“五大频道”值的数据框。由于web URL不会因不同的值而更改,因此我无法使用该URL 有人可以建议如何实现这一点吗?如果您想要自动化的东西,您可以使用Phantomjs创建csv文件,其中包含您想要的类别。然后,您可以使用python和pandas处理它。如果您想要自动化的东西,您可以使用Phantomjs创建具有所需类别的csv文件。然后您可以使用python和pand

默认情况下,在网站“”中,将显示两个下拉列表中“跨流派”和“前10名广告商”值的数据表

参考图片:

  • 我必须得到带有“运动”和“五大频道”值的数据框。由于web URL不会因不同的值而更改,因此我无法使用该URL


  • 有人可以建议如何实现这一点吗?

    如果您想要自动化的东西,您可以使用
    Phantomjs
    创建
    csv
    文件,其中包含您想要的类别。然后,您可以使用
    python
    pandas

    处理它。如果您想要自动化的东西,您可以使用
    Phantomjs
    创建具有所需类别的
    csv
    文件。然后您可以使用
    python
    pandas

    处理它。您可以
    检查浏览器上的表(
    左键单击>检查
    ),然后找到
    元素并执行
    左键单击>复制>复制元素
    。我是用Chrome做的,但你可以用任何浏览器。然后,您可以将该字符串放入变量中,并使用read_html方法使用pandas读取它

    这就是我得到的:

    html = '''<table class="stat" id="advertiser"><tbody><tr><th width="62">Rank</th><th width="135">Advertiser</th><th colspan="3">Insertions</th></tr>  <tr><th>&nbsp;</th><th>&nbsp;</th><th width="70" class="bordtop dnone">Last Week</th><th width="70" class="bordtop"> Week 30</th><th width="70" align="right" class="bordtop dnone">Gain / Loss</th>  </tr><tr><td width="62">1</td><td width="135">HINDUSTAN LEVER LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">177869</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">2</td><td width="135">RECKITT BENCKISER (INDIA) LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">42053</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">3</td><td width="135">BROOKE BOND LIPTON INDIA LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">27737</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">4</td><td width="135">PROCTER &amp; GAMBLE</td><td width="70" class="dnone">&nbsp;</td><td width="70">25208</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">5</td><td width="135">CADBURYS INDIA LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">24894</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">6</td><td width="135">SUPER CASSETTES INDUSTRIES</td><td width="70" class="dnone">&nbsp;</td><td width="70">20724</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">7</td><td width="135">PATANJALI AYURVED LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">20080</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">8</td><td width="135">GODREJ SARA LEE LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">18198</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">9</td><td width="135">GODREJ CONSUMER PRODUCTS LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">17569</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">10</td><td width="135">ITC LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">16445</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td colspan="5" class="titlebott">TOP 10 Advertiser *Across Genre : All India (U+R) : 2+ Individuals, To get this data on your Twitter timeline, tweet with #BarcTweet Top 10 Advertisers</td></tr></tbody></table>'''
    
    df = pd.read_html(html)[0]
    
    df
    
    
    0   1   2   3   4
    0   Rank    Advertiser  Insertions  NaN NaN
    1   NaN NaN Last Week   Week 30 Gain / Loss
    2   1   HINDUSTAN LEVER LTD NaN 177869  NaN
    3   2   RECKITT BENCKISER (INDIA) LTD   NaN 42053   NaN
    4   3   BROOKE BOND LIPTON INDIA LTD    NaN 27737   NaN
    5   4   PROCTER & GAMBLE    NaN 25208   NaN
    6   5   CADBURYS INDIA LTD  NaN 24894   NaN
    7   6   SUPER CASSETTES INDUSTRIES  NaN 20724   NaN
    8   7   PATANJALI AYURVED LTD   NaN 20080   NaN
    9   8   GODREJ SARA LEE LTD NaN 18198   NaN
    10  9   GODREJ CONSUMER PRODUCTS LTD    NaN 17569   NaN
    11  10  ITC LTD NaN 16445   NaN
    12  TOP 10 Advertiser *Across Genre : All India (U...   NaN NaN NaN NaN
    
    html=''上周的Rankakadvertiserinsertions 30收益/损失1印度斯坦杠杆有限公司177869 2雷克基特本奇瑟(印度)有限公司42053 3布鲁克邦德利普顿印度有限公司27737;GAMBLE 25208 5CADBURYS INDIA LTD 24894 Super Casteds INDUSTRIES 20724 Patanjali AYURVED LTD 20080 Godrej SARA LEE LTD 18198 Godrej CONSUMER PRODUCTS LTD 17569 10ITC LTD 16445十大广告商*跨类型:全印度(U+R):2+个人,要在推特时间线上获得这些数据,请与“BarcTweet十大广告商”一起推特
    df=pd.read_html(html)[0]
    df
    0   1   2   3   4
    0排名广告商插入NaN NaN
    1楠楠上周30盈/亏
    2 1印度斯坦杠杆有限公司NaN 177869 NaN
    3 2 RECKITT BENCKISER(印度)有限公司NaN 42053 NaN
    4 3布鲁克邦德立顿印度有限公司NaN 27737 NaN
    5 4宝洁南部25208南部
    6 5 CADBURYS印度有限公司NaN 24894 NaN
    7 6超级盒式磁带产业NaN 20724 NaN
    8 7 PATANJALI AYURVED有限公司NaN 20080 NaN
    9 8 GODREJ SARA LEE有限公司NaN 18198 NaN
    10 9戈德雷消费品有限公司NaN 17569 NaN
    11 10 ITC有限公司南16445南
    12个前10名广告商*跨类型:全印度(美国…南部)
    

    这可能不是最好的方法,但它只适用于一次操作。

    您可以
    检查浏览器上的表(
    左键单击>检查
    )然后找到
    元素并执行
    左键单击>复制>复制元素
    。我是用Chrome做的,但你可以用任何浏览器来做。然后你可以将该字符串放入变量中,并使用read_html方法用pandas读取它

    这就是我得到的:

    html = '''<table class="stat" id="advertiser"><tbody><tr><th width="62">Rank</th><th width="135">Advertiser</th><th colspan="3">Insertions</th></tr>  <tr><th>&nbsp;</th><th>&nbsp;</th><th width="70" class="bordtop dnone">Last Week</th><th width="70" class="bordtop"> Week 30</th><th width="70" align="right" class="bordtop dnone">Gain / Loss</th>  </tr><tr><td width="62">1</td><td width="135">HINDUSTAN LEVER LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">177869</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">2</td><td width="135">RECKITT BENCKISER (INDIA) LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">42053</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">3</td><td width="135">BROOKE BOND LIPTON INDIA LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">27737</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">4</td><td width="135">PROCTER &amp; GAMBLE</td><td width="70" class="dnone">&nbsp;</td><td width="70">25208</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">5</td><td width="135">CADBURYS INDIA LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">24894</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">6</td><td width="135">SUPER CASSETTES INDUSTRIES</td><td width="70" class="dnone">&nbsp;</td><td width="70">20724</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">7</td><td width="135">PATANJALI AYURVED LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">20080</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">8</td><td width="135">GODREJ SARA LEE LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">18198</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">9</td><td width="135">GODREJ CONSUMER PRODUCTS LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">17569</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td width="62">10</td><td width="135">ITC LTD</td><td width="70" class="dnone">&nbsp;</td><td width="70">16445</td><td width="70" class="dnone">&nbsp;</td></tr><tr><td colspan="5" class="titlebott">TOP 10 Advertiser *Across Genre : All India (U+R) : 2+ Individuals, To get this data on your Twitter timeline, tweet with #BarcTweet Top 10 Advertisers</td></tr></tbody></table>'''
    
    df = pd.read_html(html)[0]
    
    df
    
    
    0   1   2   3   4
    0   Rank    Advertiser  Insertions  NaN NaN
    1   NaN NaN Last Week   Week 30 Gain / Loss
    2   1   HINDUSTAN LEVER LTD NaN 177869  NaN
    3   2   RECKITT BENCKISER (INDIA) LTD   NaN 42053   NaN
    4   3   BROOKE BOND LIPTON INDIA LTD    NaN 27737   NaN
    5   4   PROCTER & GAMBLE    NaN 25208   NaN
    6   5   CADBURYS INDIA LTD  NaN 24894   NaN
    7   6   SUPER CASSETTES INDUSTRIES  NaN 20724   NaN
    8   7   PATANJALI AYURVED LTD   NaN 20080   NaN
    9   8   GODREJ SARA LEE LTD NaN 18198   NaN
    10  9   GODREJ CONSUMER PRODUCTS LTD    NaN 17569   NaN
    11  10  ITC LTD NaN 16445   NaN
    12  TOP 10 Advertiser *Across Genre : All India (U...   NaN NaN NaN NaN
    
    html=''RankAdvertiserInsertions上周30收益/损失1印度斯坦杠杆有限公司177869 2雷克基特·本奇瑟(印度)有限公司42053 Brooke BOND LIPTON INDIA LTD 27737 Procter&GAMBLE 25208 Cadburys INDIA LTD 24894超级卡带产业20724 Patanjali AYURVED LTD 20080 Godrej SARA LEE LTD 18198 Godrej消费品有限公司17569 10ITC LTD 16445十大广告商*跨类型:全印度(U+R):2+个人,要在推特时间线上获取这些数据,请与#BarcTweet前10名广告商‘’一起推特
    df=pd.read_html(html)[0]
    df
    0   1   2   3   4
    0排名广告商插入NaN NaN
    1楠楠上周30盈/亏
    2 1印度斯坦杠杆有限公司NaN 177869 NaN
    3 2 RECKITT BENCKISER(印度)有限公司NaN 42053 NaN
    4 3布鲁克邦德立顿印度有限公司NaN 27737 NaN
    5 4宝洁南部25208南部
    6 5 CADBURYS印度有限公司NaN 24894 NaN
    7 6超级盒式磁带产业NaN 20724 NaN
    8 7 PATANJALI AYURVED有限公司NaN 20080 NaN
    9 8 GODREJ SARA LEE有限公司NaN 18198 NaN
    10 9戈德雷消费品有限公司NaN 17569 NaN
    11 10 ITC有限公司南16445南
    12个前10名广告商*跨类型:全印度(美国…南部)
    
    这可能不是最好的方法,但它只适用于一次