Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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
使用Selenium(Python)删除符号_Python_Mysql_Pandas_Selenium_Web Scraping - Fatal编程技术网

使用Selenium(Python)删除符号

使用Selenium(Python)删除符号,python,mysql,pandas,selenium,web-scraping,Python,Mysql,Pandas,Selenium,Web Scraping,下面是一个selenium web scraper,它在不同的选项卡之间循环,选择“导出数据”按钮,下载数据,添加“yearid”列,然后将数据加载到MySQL表中 import sys import pandas as pd import os import time from datetime import datetime from selenium import webdriver from selenium.webdriver.firefox.firefox_profile impor

下面是一个selenium web scraper,它在不同的选项卡之间循环,选择“导出数据”按钮,下载数据,添加“yearid”列,然后将数据加载到MySQL表中

import sys
import pandas as pd
import os
import time
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from sqlalchemy import create_engine


button_text_to_url_type = {
    'dashboard': 8,
    'standard': 0,
     'advanced': 1,
     'batted_ball': 2,
     'win_probability': 3,
     'pitch_type': 4,
     'pitch_values': 7,
     'plate_discipline': 5,
     'value': 6
}

download_dir = os.getcwd()
profile = FirefoxProfile("C:/Users/PATHTOFIREFOX")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'text/csv')
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", download_dir)
profile.set_preference("browser.download.folderList", 2)
driver = webdriver.Firefox(firefox_profile=profile)


today = datetime.today()
for button_text, url_type in button_text_to_url_type.items():

    default_filepath = os.path.join(download_dir, 'Fangraphs Leaderboard.csv')
    desired_filepath = os.path.join(download_dir,
                                    '{}_{}_{}_Leaderboard_{}.csv'.format(today.year, today.month, today.day,
                                                                         button_text))

    driver.get(
        "https://www.fangraphs.com/leaders.aspx?pos=all&stats=bat&lg=all&qual=0&type={}&season=2018&month=0&season1=2018&ind=0&team=&rost=&age=&filter=&players=".format(
            url_type))
    driver.find_element_by_link_text('Export Data').click()
    if os.path.isfile(default_filepath):
        os.rename(default_filepath, desired_filepath)
        print('Renamed file {} to {}'.format(default_filepath, desired_filepath))
    else:
        sys.exit('Error, unable to locate file at {}'.format(default_filepath))

    df = pd.read_csv(desired_filepath)
    df.str.replace('%', '')
    df["yearid"] = datetime.today().year
    df.to_csv(desired_filepath)

    engine = create_engine("mysql+pymysql://{user}:{pw}@localhost/{db}"
                           .format(user="walker",
                                   pw="password",
                                   db="data"))
    df.to_sql(con=engine, name='fg_test_hitting_{}'.format(button_text), if_exists='replace')

time.sleep(10)
driver.quit()

scraper工作得很好,但是,当我下载数据时,有些列下载的数据在整数(即25%)后加上%号,这就抛弃了我在MySQL中的数据类型。我尝试过使用df.str.replace('%','')和df.replace('%',''),但都没有成功。将数据刮入数据框时,是否可以更改包含%符号的列,使其仅显示整数?我应该将其合并到我的循环中,还是可以在建立数据帧后添加一行代码?提前谢谢

df.replace
不进行就地更换。它返回修改后的数据帧 所以换掉这个

df.str.replace('%', '')


谢谢你的回复Sunitha!不幸的是,我一直得到错误“DataFrame”对象没有字符串。你知道为什么会出现这个吗?对不起。。不知道您正在使用熊猫数据帧。我已经更新了我的帖子。请尝试一下新的解决方案,效果不错!非常感谢你,苏尼莎!还应注意,我必须将冒号改为逗号。。那是个打字错误。。。修好了
df = df.replace('%', '', regex=True)