Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.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_Pandas_Dataframe_Beautifulsoup - Fatal编程技术网

Python 使用字符串生成数据帧

Python 使用字符串生成数据帧,python,pandas,dataframe,beautifulsoup,Python,Pandas,Dataframe,Beautifulsoup,各位好, 我尝试将dataframe列设为“Date”,并将datetimes添加到列行中。datetime在字符串列表中每隔5次出现一次 我认为像范围(开始、结束、步骤)这样的方法是好的,但在实践中如何做到呢 这是我的密码: import requests, re, pandas from bs4 import BeautifulSoup r=requests.get("http://www.hltv.org/?pageid=188&statsfilter=2816&offs

各位好,

我尝试将dataframe列设为“Date”,并将datetimes添加到列行中。datetime在字符串列表中每隔5次出现一次

我认为像范围(开始、结束、步骤)这样的方法是好的,但在实践中如何做到呢

这是我的密码:

import requests, re, pandas
from bs4 import BeautifulSoup

r=requests.get("http://www.hltv.org/?pageid=188&statsfilter=2816&offset=0")
c=r.content

soup=BeautifulSoup(c,"html.parser")


for string in soup.find_all("div",{"class":"covSmallHeadline"})[6:]:
    print(string.text.replace("(","").replace(")",""))
下面是输出(实际列表大小更大):


首先将数据转换为CSV:

import re
In [83]: for row in table.find_all('div', style=re.compile(r'width:606px;height:22px;background-color')):
   ...:      print(row.get_text(strip=True, separator=','))


5/3 17,Astralis (16),FaZe (13),inferno,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),nuke,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),overpass,IEM Katowice 2017

请澄清您希望从输入中获得哪种类型的输出(数据帧、系列)?我现在还不清楚。我试着让pandas.DataFrame输出。因为学习,我会问这行是什么:'宽度:606px;高度:22px;背景色“实际上是吗?”@Juho M这是样式属性,我用这个字符串定位了每一行。因为每行的样式不同,所以我使用
re
来匹配它们的相同部分。
import re
In [83]: for row in table.find_all('div', style=re.compile(r'width:606px;height:22px;background-color')):
   ...:      print(row.get_text(strip=True, separator=','))


5/3 17,Astralis (16),FaZe (13),inferno,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),nuke,IEM Katowice 2017
5/3 17,Astralis (16),FaZe (12),overpass,IEM Katowice 2017